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?
Org Mode (also: org-mode; /ˈɔːrɡ moʊd/) is a document editing, formatting, and organizing mode, designed for notes, planning, and authoring within the free software text editor Emacs.
Org mode is routinely used to build and manage complex workflows. It does this using an elegantly simple syntax that scales from basic markup to full LaTeX typesetting and from plain text notes to literate programs. Everything you need to get started is demonstrated in the example.
The easiest way to create a table is to directly type the "|" character at the beginning of a line, or after any amount of white space. This will put you in the first field of an atomic table. Once you've finished editing this cell, you can jump to the next one by pressing TAB .
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-regexp
RET \(.*\),\(.*\),\(.*\),\(.*\)
RET :PROPERTIES:
C-q C-j :Name: \1
C-q C-j :Tel: \2
C-q C-j :Mobile: \3
C-q C-j :Fax: \4
C-q C-j :END:
RET
If 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.
use csv-mode, transpose rows and columns by csv-transpose and format with replace-regexp:
search \(.*\),\(.*\)
replace for: :\1: \2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With