Org-mode mailing list
 help / color / Atom feed
* Converting csv (with new lines) to org-mode.
@ 2013-01-31 12:25 Nicolas Richard
  2013-02-15 21:29 ` Bastien
  0 siblings, 1 reply; 2+ messages in thread
From: Nicolas Richard @ 2013-01-31 12:25 UTC (permalink / raw)
  To: emacs-orgmode

Hello everyone,

A csv table can include newlines in its fields, which confuses the csv
parser contained in org-table-convert-region. Since I had no time to
patch the current implementation of org-table-convert-region, I decided
to use an already existing csv parser found in marmalade. You find below
the code I used (requires pcsv, a csv parser) if anyone is interested.
It's very simple : pcsv does the work of transforming the csv into a
list of list, then I mapconcat those using " | " as separator.

(defun yf/lisp-table-to-org-table (table &optional function)
  "Convert a lisp table to `org-mode' syntax, applying FUNCTION to each of its elements.
The elements should not have any more newlines in them after
applying FUNCTION ; the default converts them to spaces. Return
value is a string containg the unaligned `org-mode' table."
  (unless (functionp function)
    (setq function (lambda (x) (replace-regexp-in-string "\n" " " x))))
  (mapconcat (lambda (x)                ; x is a line.
               (concat "| " (mapconcat function x " | ") " |"))
             table "\n"))

(defun yf/csv-to-table (beg end)
"Convert a csv file to an `org-mode' table."
  (interactive "r")
  (require 'pcsv)
  (insert (yf/lisp-table-to-org-table (pcsv-parse-region beg end)))
  (delete-region beg end)
  (org-table-align))

-- 
N.

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

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-01-31 12:25 Converting csv (with new lines) to org-mode Nicolas Richard
2013-02-15 21:29 ` Bastien

Org-mode mailing list

Archives are clonable:
	git clone --mirror https://orgmode.org/list/0 list/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 list list/ https://orgmode.org/list \
		emacs-orgmode@gnu.org
	public-inbox-index list

Example config snippet for mirrors

Newsgroups are available over NNTP:
	nntp://news.yhetil.org/yhetil.emacs.orgmode
	nntp://news.gmane.io/gmane.emacs.orgmode


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git