unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* exporting nested list to CSV file
@ 2014-07-22 18:52 Christopher Howard
  2014-07-22 19:35 ` Thorsten Jolitz
  0 siblings, 1 reply; 5+ messages in thread
From: Christopher Howard @ 2014-07-22 18:52 UTC (permalink / raw)
  To: Gnu Emacs Help

Hi. I've been using the pcsv library to parse data from external CSV
files, then using various elisp functions to filter/munge the
data. Afterwards, I was sad to discover that pcsv does not have any
functions for /writing/ data back to a CSV file.

Since the data is well-structured, presumably this shouldn't be hard
to do. Could I get some guidance on what would be the simplest
approach? Is there another library I've overlooked? Or some built-ins
that would be especially helpful? The data are in lists like so:

(("row0datastring", "row0datastring", "row0datastring", ...)
 ("row1datastring", "row1datastring", "row1datastring", ...)
 ...
)



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

* Re: exporting nested list to CSV file
  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
  0 siblings, 1 reply; 5+ messages in thread
From: Thorsten Jolitz @ 2014-07-22 19:35 UTC (permalink / raw)
  To: help-gnu-emacs

Christopher Howard <cmhoward2@alaska.edu> writes:

> Hi. I've been using the pcsv library to parse data from external CSV
> files, then using various elisp functions to filter/munge the
> data. Afterwards, I was sad to discover that pcsv does not have any
> functions for /writing/ data back to a CSV file.
>
> Since the data is well-structured, presumably this shouldn't be hard
> to do. Could I get some guidance on what would be the simplest
> approach? Is there another library I've overlooked? Or some built-ins
> that would be especially helpful? The data are in lists like so:
>
> (("row0datastring", "row0datastring", "row0datastring", ...)
>  ("row1datastring", "row1datastring", "row1datastring", ...)
>  ...
> )

try something like:

#+begin_src emacs-lisp 
  (mapconcat
   'identity
   (append
    '("row0datastring" "row0datastring" "row0datastring")
    '("row1datastring" "row1datastring" "row1datastring")) ",")
#+end_src

#+results:
: row0datastring,row0datastring,row0datastring,row1datastring,row1datastring,row1datastring

-- 
cheers,
Thorsten




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

* Re: exporting nested list to CSV file
  2014-07-22 19:35 ` Thorsten Jolitz
@ 2014-07-22 22:25   ` Christopher Howard
  2014-07-22 23:59     ` Christopher Howard
  0 siblings, 1 reply; 5+ messages in thread
From: Christopher Howard @ 2014-07-22 22:25 UTC (permalink / raw)
  To: help-gnu-emacs

On Tue, 22 Jul 2014 21:35:35 +0200
Thorsten Jolitz <tjolitz@gmail.com> wrote:

> Christopher Howard <cmhoward2@alaska.edu> writes:
> 
> > Hi. I've been using the pcsv library to parse data from external CSV
> > files, then using various elisp functions to filter/munge the
> > data. Afterwards, I was sad to discover that pcsv does not have any
> > functions for /writing/ data back to a CSV file.
> >
> > Since the data is well-structured, presumably this shouldn't be hard
> > to do. Could I get some guidance on what would be the simplest
> > approach? Is there another library I've overlooked? Or some
> > built-ins that would be especially helpful? The data are in lists
> > like so:
> >
> > (("row0datastring", "row0datastring", "row0datastring", ...)
> >  ("row1datastring", "row1datastring", "row1datastring", ...)
> >  ...
> > )
> 
> try something like:
> 
> #+begin_src emacs-lisp 
>   (mapconcat
>    'identity
>    (append
>     '("row0datastring" "row0datastring" "row0datastring")
>     '("row1datastring" "row1datastring" "row1datastring")) ",")
> #+end_src
> 
> #+results:
> :
> row0datastring,row0datastring,row0datastring,row1datastring,row1datastring,row1datastring
> 

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



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

* Re: exporting nested list to CSV file
  2014-07-22 22:25   ` Christopher Howard
@ 2014-07-22 23:59     ` Christopher Howard
  2014-07-23  6:19       ` Nicolas Richard
  0 siblings, 1 reply; 5+ messages in thread
From: Christopher Howard @ 2014-07-22 23:59 UTC (permalink / raw)
  To: help-gnu-emacs

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



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

* Re: exporting nested list to CSV file
  2014-07-22 23:59     ` Christopher Howard
@ 2014-07-23  6:19       ` Nicolas Richard
  0 siblings, 0 replies; 5+ messages in thread
From: Nicolas Richard @ 2014-07-23  6:19 UTC (permalink / raw)
  To: Christopher Howard; +Cc: help-gnu-emacs

Christopher Howard <cmhoward2@alaska.edu> writes:
> (defun csv-quote-field (data)
>   (mapconcat
>    'identity
>    `("\""
>      ,(s-replace-all
>        '(("\"" . "\"\"")) data)
>      "\"") ""))

aka
(defun csv-quote-field (data)
  (format "\"%s\"" (s-replace-all '(("\"" . "\"\"")) data)))
IIUC.

-- 
Nico.



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

end of thread, other threads:[~2014-07-23  6:19 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2014-07-23  6:19       ` Nicolas Richard

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