emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Nick Dokos <nicholas.dokos@hp.com>
To: Samuel Wales <samologist@gmail.com>
Cc: nicholas.dokos@hp.com, emacs-orgmode@gnu.org
Subject: Re: [OT] CSV to Org
Date: Tue, 22 Nov 2011 18:31:50 -0500	[thread overview]
Message-ID: <14656.1322004710@alphaville.americas.hpqcorp.net> (raw)
In-Reply-To: Message from Samuel Wales <samologist@gmail.com> of "Tue, 22 Nov 2011 15:56:33 MST." <CAJcAo8vkeGeuu5Be7GCsg994xjyrLGktBdebKOKA8fju0HqQKQ@mail.gmail.com>

Samuel Wales <samologist@gmail.com> 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

  reply	other threads:[~2011-11-22 23:31 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-22 22:56 [OT] CSV to Org Samuel Wales
2011-11-22 23:31 ` Nick Dokos [this message]
2011-11-23  1:16   ` Samuel Wales
2011-11-23  1:19     ` Samuel Wales
2011-11-23  5:37     ` Nick Dokos
2011-11-30 12:13 ` Eric S Fraga
2011-11-30 16:30   ` Eric Schulte

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=14656.1322004710@alphaville.americas.hpqcorp.net \
    --to=nicholas.dokos@hp.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=samologist@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).