views:

908

answers:

4

Emacs has a very nice extension by the name of org-mode.

I would like to be able to easily load CSV files into org-mode without significant grief. All I've been able to find is table-import or table-capture, which, simply put, don't work even approximately well.

Note that part of my issue is text strings with a comma within them. 1,2,3,4 is different than 1,2,"3,4".

Is there a function out there or a perl script that one could run to transform a csv file into org-mode format?

Thanks!

+2  A: 

I'm assuming you want to convert your CSV specifically into org-mode tables. If that's not the case, you may want to be more explicit about output format in your question.

Something like this should do it, or at least get you a starting point you can hack on:

  #!/usr/bin/perl

  use strict;
  use warnings;

  use Text::CSV;

  my $csv = Text::CSV->new();

  while ( my $line = <DATA> ) {
    if ( $csv->parse( $line )) {
      my $str = join '|' , $csv->fields();
      print "|$str|\n";
    }
  }


  __DATA__
  1,2,3,4
  1,2,"3,4"
genehack
+3  A: 

From the org-mode manual:

C-c | Convert the active region to table. If every line contains at least one TAB character, the function assumes that the material is tab separated. If every line contains a comma, comma-separated values (CSV) are assumed. If not, lines are split at whitespace into elds. You can use a prex argument to force a specic separator: C-u forces CSV, C-u C-u forces TAB, and a numeric argument N indicates that at least N consecutive spaces, or alternatively a TAB will be the separator. If there is no active region, this command creates an empty Org table.

So just paste the data into an org file, select it, and do C-u C-c | .

Don Womick
Hmm, looks like the "fi" ligatures in the PDF got dropped out of the quote above... anyway... "elds" should be "fields," "specic" should be "specific," and "prex" should be "prefix."
Don Womick
Doesn't work when you have CSV fields that contain commas, which the OP specifies...
genehack
A: 

Try this:

;; Insert a file and convert it to an org table
(defun aleblanc/insert-file-as-org-table (filename)
  "Insert a file into the current buffer at point, and convert it to an org table."
  (interactive (list (ido-read-file-name "csv file: ")))
  (let* ((start (point))
    (end (+ start (nth 1 (insert-file-contents filename)))))
    (org-table-convert-region start end)
    ))
Aleblanc
+1  A: 

Here is a bit of a hack-job, but it works.

when you export the CSV file, force quotes around each entry, then replace all "," as a vertical bar.

Finally, I make a macro that does something like this:

C-a    ; Beginning-of-line
|      ; Self-insert-char
C-e    ; end-of-line
|      ; Self-insert-char
<down> ; Down one line

(I am not 100% sure if | is a self-insert-char or not, so its best to record your own macro)

Hit tab somewhere in the middle of the table, and org-mode formats it properly. I find this easier to do in a narrowed region.

Caveat: If you have a vertical bar in your input.. it probably won't work quite right. Situations like that should be easy to spot, and relatively easy to fix.

Generally, when importing something text-like into org-mode, I have found a combination of some smart macro writing, and search-replace to be the easiest way

I hope that helps.

Jonathan Arkell