emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [OT] CSV to Org
@ 2011-11-22 22:56 Samuel Wales
  2011-11-22 23:31 ` Nick Dokos
  2011-11-30 12:13 ` Eric S Fraga
  0 siblings, 2 replies; 7+ messages in thread
From: Samuel Wales @ 2011-11-22 22:56 UTC (permalink / raw)
  To: emacs-orgmode

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.

I am aware of the table importer.

Thanks.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [OT] CSV to Org
  2011-11-22 22:56 [OT] CSV to Org Samuel Wales
@ 2011-11-22 23:31 ` Nick Dokos
  2011-11-23  1:16   ` Samuel Wales
  2011-11-30 12:13 ` Eric S Fraga
  1 sibling, 1 reply; 7+ messages in thread
From: Nick Dokos @ 2011-11-22 23:31 UTC (permalink / raw)
  To: Samuel Wales; +Cc: nicholas.dokos, emacs-orgmode

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [OT] CSV to Org
  2011-11-22 23:31 ` Nick Dokos
@ 2011-11-23  1:16   ` Samuel Wales
  2011-11-23  1:19     ` Samuel Wales
  2011-11-23  5:37     ` Nick Dokos
  0 siblings, 2 replies; 7+ messages in thread
From: Samuel Wales @ 2011-11-23  1:16 UTC (permalink / raw)
  To: nicholas.dokos; +Cc: emacs-orgmode

Hmm, thanks for your comments.  I can see the appeal of a Python-ish
solution, but I was thinking maybe some elisp (or even a package or a
part of Org) instead.  Then it could grow to a generally useful
importer.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [OT] CSV to Org
  2011-11-23  1:16   ` Samuel Wales
@ 2011-11-23  1:19     ` Samuel Wales
  2011-11-23  5:37     ` Nick Dokos
  1 sibling, 0 replies; 7+ messages in thread
From: Samuel Wales @ 2011-11-23  1:19 UTC (permalink / raw)
  To: nicholas.dokos; +Cc: emacs-orgmode

Hi Nick,

Thanks for your comments.  I can see the appeal of an external tool,
but I was thinking maybe some elisp (or even a package or a
part of Org) could do it instead.  Then it could maybe even grow to
become a generally useful
importer.  I am useless at Python.

Samuel

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [OT] CSV to Org
  2011-11-23  1:16   ` Samuel Wales
  2011-11-23  1:19     ` Samuel Wales
@ 2011-11-23  5:37     ` Nick Dokos
  1 sibling, 0 replies; 7+ messages in thread
From: Nick Dokos @ 2011-11-23  5:37 UTC (permalink / raw)
  To: Samuel Wales; +Cc: nicholas.dokos, emacs-orgmode

Samuel Wales <samologist@gmail.com> wrote:

> Hmm, thanks for your comments.  I can see the appeal of a Python-ish
> solution, but I was thinking maybe some elisp (or even a package or a
> part of Org) instead.  Then it could grow to a generally useful
> importer.
> 

The trouble is that CSV is a very loosely defined format, with many
variations and dialects. The nice thing about the python solution is
that it takes care of many of these variations.

If somebody does take up your challenge, I would strongly advise that
they should consult the csv python module (or equivalent modules in
other languages, depending on their preferred language) and the design
document for it (PEP 305 - at http://www.python.org/dev/peps/pep-0305/ )
and try to translate it into elisp, rather than striking off on their
own.

Nick

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [OT] CSV to Org
  2011-11-22 22:56 [OT] CSV to Org Samuel Wales
  2011-11-22 23:31 ` Nick Dokos
@ 2011-11-30 12:13 ` Eric S Fraga
  2011-11-30 16:30   ` Eric Schulte
  1 sibling, 1 reply; 7+ messages in thread
From: Eric S Fraga @ 2011-11-30 12:13 UTC (permalink / raw)
  To: Samuel Wales; +Cc: emacs-orgmode

Samuel Wales <samologist@gmail.com> writes:

> I have an old CSV that looks like this.  I'd like to convert
> it to Org format.

I've used csv-mode in the past for this but I cannot do this now as
there is some bug in csv-mode that is causing problems with, of all
things, my mode-line-format...  sigh.

Anyway, check out that mode in case it gives you enough to then import
the result as an org table.

HTH,
eric
-- 
: Eric S Fraga (GnuPG: 0xC89193D8FFFCF67D) in Emacs 24.0.90.1
: using Org-mode version 7.7 (release_7.7.381.g05ea.dirty)

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [OT] CSV to Org
  2011-11-30 12:13 ` Eric S Fraga
@ 2011-11-30 16:30   ` Eric Schulte
  0 siblings, 0 replies; 7+ messages in thread
From: Eric Schulte @ 2011-11-30 16:30 UTC (permalink / raw)
  To: Samuel Wales; +Cc: emacs-orgmode

I'm coming to this thread late, so forgive me if this has already been
mentioned, but `org-table-import' always works for me.

Best,

-- 
Eric Schulte
http://cs.unm.edu/~eschulte/

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2011-11-30 16:31 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-11-22 22:56 [OT] CSV to Org Samuel Wales
2011-11-22 23:31 ` Nick Dokos
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

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).