all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Christopher Howard <cmhoward2@alaska.edu>
To: help-gnu-emacs@gnu.org
Subject: Re: exporting nested list to CSV file
Date: Tue, 22 Jul 2014 15:59:04 -0800	[thread overview]
Message-ID: <20140722155904.6dac25b4@voltron.arsc.edu> (raw)
In-Reply-To: <20140722142517.3a7bee43@voltron.arsc.edu>

On Tue, 22 Jul 2014 14:25:17 -0800
Christopher Howard <cmhoward2@alaska.edu> wrote:

> 
> Thank you for helping me get started. I also needed to address the
> issues of 1) newlines at the end of rows and 2) character escaping. To
> address the second issue, I used "prin1", which I hope is a good
> enough hack for my purposes:
> 
> #+begin_src emacs-lisp
> (defun to-csv-row (fields)
>   (mapconcat 'prin1-to-string fields ","))
> 
> (defun to-csv-string (nested-list)
>   (mapconcat 'to-csv-row nested-list "\n"))
> 
> (defun to-csv-file (path nested-list)
>   "Converts NESTED-LIST to csv format and exports to file.
> NESTED-LIST is expected to be the structure provided by the pcsv
> library." (with-temp-file path
>     (insert (to-csv-string nested-list))))
> #+end_src

For posterity: CSV using repetition to escape quotes inside quotes:

#+begin_src emacs-lisp
(defun csv-quote-field (data)
  (mapconcat
   'identity
   `("\""
     ,(s-replace-all
       '(("\"" . "\"\"")) data)
     "\"") ""))

(defun to-csv-row (fields)
  (mapconcat 'csv-quote-field fields ","))

(defun to-csv-string (nested-list)
  (mapconcat 'to-csv-row nested-list "\n"))

(defun to-csv-file (path nested-list)
  "Converts NESTED-LIST to csv format and exports to file. NESTED-LIST is
   expected to be the structure provided by the pcsv library."
  (with-temp-file path
    (insert (to-csv-string nested-list))))
#+end_src



  reply	other threads:[~2014-07-22 23:59 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-22 18:52 exporting nested list to CSV file Christopher Howard
2014-07-22 19:35 ` Thorsten Jolitz
2014-07-22 22:25   ` Christopher Howard
2014-07-22 23:59     ` Christopher Howard [this message]
2014-07-23  6:19       ` Nicolas Richard

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

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

  git send-email \
    --in-reply-to=20140722155904.6dac25b4@voltron.arsc.edu \
    --to=cmhoward2@alaska.edu \
    --cc=help-gnu-emacs@gnu.org \
    /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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.