emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] [OT] CSV to Org


From: Nick Dokos
Subject: Re: [O] [OT] CSV to Org
Date: Tue, 22 Nov 2011 18:31:50 -0500

Samuel Wales <address@hidden> wrote:

> I have an old CSV that looks like this.  I'd like to convert
> it to Org format.
> 
> I think I want to use properties for every field except Name
> (headline) and Note (body text with \r\n meaning newline).
> 
> What is the best way to do this?
> 
> "Na&me","&W","&H","A","B","N&et","&File","&Title","&Org","Addr&1","C&i","&St","&Zip","Addr&2","&Pc","&Note","gp","a&k","g&c","&Do","g&a"
> "some name something","823-2323","233-2323 as a a a a as","2323
> something","","2323 fax?  3333
> fax!","soao-sss.ss","","","","","","","","","",0,0,0,0,0
> "bob and sylvia
> whosit","","","","","address@hidden","","","","","","ca","","","","friend
> sss\r\nwwwww\r\nzsdddddd\r\nwwww\r\n",0,0,0,0,0
> ...
> 
> More generally, I wonder if there is a general mechanism, as I imagine
> this (with variations) is pretty common.
> 

Personally, I'd write a little python program to do the transformation.
There is a csv module that turns a record into a list of fields. IIRC,
there is also one that turns a record into a dict, so you can get the fields
by name. I can dig further if you are interested.

The following code comes straight from the docs:

--8<---------------cut here---------------start------------->8---
import csv
reader = csv.reader(open("foo.csv", "rb"))
for row in reader:
    print row
--8<---------------cut here---------------end--------------->8---

On your example, it gives

,----
| ['Na&me', '&W', '&H', 'A', 'B', 'N&et', '&File', '&Title', '&Org', 'Addr&1', 
'C&i', '&St', '&Zip', 'Addr&2', '&Pc', '&Note', 'gp', 'a&k', 'g&c', '&Do', 
'g&a']
| ['some name something', '823-2323', '233-2323 as a a a a as', 
'2323\nsomething', '', '2323 fax?  3333\nfax!', 'soao-sss.ss', '', '', '', '', 
'', '', '', '', '', '0', '0', '0', '0', '0']
| ['bob and sylvia\nwhosit', '', '', '', '', 'address@hidden', '', '', '', '', 
'', 'ca', '', '', '', 'friend\nsss\\r\\nwwwww\\r\\nzsdddddd\\r\\nwwww\\r\\n', 
'0', '0', '0', '0', '0']
`----

So row[0] is the name, row[15] is the note, etc. Ruby, perl, etc. can
probably do it easily too.

You probably want something like this, with obvious extensions for other 
properties -
but note that cr/nl handling leaves a lot to be desired:

--8<---------------cut here---------------start------------->8---
import csv
reader = csv.reader(open("foo.csv", "rb"))
titles = reader.next()
for row in reader:
    print "* %s\n:PROPERTIES:\n:%s:\t%s\n:END:\n\n%s\n" % (row[0], titles[1], 
row[1], row[15])
--8<---------------cut here---------------end--------------->8---

Output:

,----
| * some name something
| :PROPERTIES:
| :&W:  823-2323
| :END:
| 
| 
| 
| * bob and sylvia
| whosit
| :PROPERTIES:
| :&W:  
| :END:
| 
| friend
| sss\r\nwwwww\r\nzsdddddd\r\nwwww\r\n
| 
`----

(This is python 2.6 btw - things are different in python 3).

HTH,
Nick




reply via email to

[Prev in Thread] Current Thread [Next in Thread]