Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import CSV into Org-mode properties

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?

like image 643
lecodesportif Avatar asked Apr 06 '10 00:04

lecodesportif


People also ask

What are org mode files?

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.

What is ORG mode good for?

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.

How do you make a table in Org mode?

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 .


2 Answers

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.

like image 54
phils Avatar answered Sep 21 '22 18:09

phils


use csv-mode, transpose rows and columns by csv-transpose and format with replace-regexp:

search \(.*\),\(.*\)

replace for: :\1: \2

like image 23
user310031 Avatar answered Sep 23 '22 18:09

user310031