From mboxrd@z Thu Jan 1 00:00:00 1970 From: Nick Dokos Subject: Re: [OT] CSV to Org Date: Tue, 22 Nov 2011 18:31:50 -0500 Message-ID: <14656.1322004710@alphaville.americas.hpqcorp.net> References: Reply-To: nicholas.dokos@hp.com Return-path: Received: from eggs.gnu.org ([140.186.70.92]:41736) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RSzog-0004cW-Jg for emacs-orgmode@gnu.org; Tue, 22 Nov 2011 18:31:55 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RSzoe-0004EL-S4 for emacs-orgmode@gnu.org; Tue, 22 Nov 2011 18:31:54 -0500 Received: from g1t0027.austin.hp.com ([15.216.28.34]:44817) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RSzoe-0004EG-Lc for emacs-orgmode@gnu.org; Tue, 22 Nov 2011 18:31:52 -0500 In-Reply-To: Message from Samuel Wales of "Tue, 22 Nov 2011 15:56:33 MST." List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Samuel Wales Cc: nicholas.dokos@hp.com, emacs-orgmode@gnu.org Samuel Wales 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","","","","","sylvia@sssssss.stanford.edu","","","","","","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', '', '', '', '', 'sylvia@sssssss.stanford.edu', '', '', '', '', '', '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