From mboxrd@z Thu Jan 1 00:00:00 1970 From: "Nicolas Richard" Subject: Converting csv (with new lines) to org-mode. Date: Thu, 31 Jan 2013 13:25:45 +0100 Message-ID: <87a9rpy1x2.fsf@yahoo.fr> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([208.118.235.92]:33093) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U0tD6-0005X9-FC for emacs-orgmode@gnu.org; Thu, 31 Jan 2013 07:25:45 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1U0tD5-0005hF-60 for emacs-orgmode@gnu.org; Thu, 31 Jan 2013 07:25:44 -0500 Received: from plane.gmane.org ([80.91.229.3]:32786) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U0tD4-0005h7-N2 for emacs-orgmode@gnu.org; Thu, 31 Jan 2013 07:25:42 -0500 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1U0tDJ-0007uU-DK for emacs-orgmode@gnu.org; Thu, 31 Jan 2013 13:25:57 +0100 Received: from geodiff-mac3.ulb.ac.be ([164.15.131.113]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 31 Jan 2013 13:25:57 +0100 Received: from theonewiththeevillook by geodiff-mac3.ulb.ac.be with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 31 Jan 2013 13:25:57 +0100 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: emacs-orgmode@gnu.org 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.