I would like to import a CSV into Org-mode. Others have already asked about importing CSV to Org-mode tables. That’s not what I am trying to do. I need to import CSV to Org-mode properties.
For example, a CSV like this:
Name,Tel,Mobile,Fax
John,11111,22222,33333
should become:
:PROPERTIES:
:Name: John
:Tel: 11111
:Mobile: 22222
:Fax: 33333
:END:
Do you happen to know a painless way to do it?
The easiest approach I can see is to mark the data rows as the region, and then use a regexp search and replace:
M-x
replace-regexpRET\(.*\),\(.*\),\(.*\),\(.*\)RET:PROPERTIES:C-q C-j:Name: \1C-q C-j:Tel: \2C-q C-j:Mobile: \3C-q C-j:Fax: \4C-q C-j:END:RETIf you needed to do this for many variable CSV files with different headers and numbers of columns, then I would probably approach it with keyboard macros.
user310031’s answer would make a good basis for that. The macro could narrow the buffer to each row, insert the header row above it, perform the
csv-transpose(which appears to require CSV mode) do the search+replace, add in the:PROPERTIES:and:END:lines, widen the buffer again, and leave point on the line before the next data row. Then just mark the remaining data rows as the region, and type C-x C-k r.