From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Christopher Howard Newsgroups: gmane.emacs.help Subject: Re: exporting nested list to CSV file Date: Tue, 22 Jul 2014 15:59:04 -0800 Message-ID: <20140722155904.6dac25b4@voltron.arsc.edu> References: <20140722105221.254307f2@voltron.arsc.edu> <87oawhchko.fsf@gmail.com> <20140722142517.3a7bee43@voltron.arsc.edu> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1406073487 30525 80.91.229.3 (22 Jul 2014 23:58:07 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 22 Jul 2014 23:58:07 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Jul 23 01:58:00 2014 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1X9jwW-0000Pa-6s for geh-help-gnu-emacs@m.gmane.org; Wed, 23 Jul 2014 01:58:00 +0200 Original-Received: from localhost ([::1]:42084 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X9jwV-0000Zx-Rr for geh-help-gnu-emacs@m.gmane.org; Tue, 22 Jul 2014 19:57:59 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:51466) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X9jwF-0000Zf-Kk for help-gnu-emacs@gnu.org; Tue, 22 Jul 2014 19:57:49 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1X9jw9-0007Vt-CN for help-gnu-emacs@gnu.org; Tue, 22 Jul 2014 19:57:43 -0400 Original-Received: from mail-pa0-f42.google.com ([209.85.220.42]:49683) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X9jw9-0007Vl-72 for help-gnu-emacs@gnu.org; Tue, 22 Jul 2014 19:57:37 -0400 Original-Received: by mail-pa0-f42.google.com with SMTP id lf10so511293pab.29 for ; Tue, 22 Jul 2014 16:57:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:subject:message-id:in-reply-to :references:mime-version:content-type:content-transfer-encoding; bh=ql2w8q0ofmt1sEWWwWwF1aIIY+0jNVlkzTkQ26yF/p8=; b=IAT/urB549zAaymoyOKgHGbUSU/08lrqfxOF7i35U7QeRqa/Z2nurZkkPIkK7+9hop rSY63Sx1SC+sJnXoWI78tQuiXIixSmtdEN0Xo1DV2D9QGW7FcfNacZaUnilet/FQXHqR 46uS8fhbFrb1nOMKZ+sBDtYPNglyaoA1VoE2R1GonYtp+67CT1AsVERRl0yxvgYOOc5A kGru9BRvZxkuTQBGHxjcGf/vlv09arQlqFNoE4ti2hWB4LWHlyv9tGQzAXJ0zrrzTlwr b4oWrW2XwxxKmldZNlycFbcqmMai6Qe6P0hYXE5UCGSTt/pNDQcVICigyEOSfYZhD+Ml 9hAA== X-Gm-Message-State: ALoCoQmjp2rZUIZgLNlHTiNjOpk4gEePtlQ4h1Wlrg9K2MQ3/3y0qVprU9CgXeuUGbTOjz8MXppe X-Received: by 10.66.121.168 with SMTP id ll8mr38431602pab.86.1406073456208; Tue, 22 Jul 2014 16:57:36 -0700 (PDT) Original-Received: from voltron.arsc.edu ([199.165.81.33]) by mx.google.com with ESMTPSA id cr4sm375200pbb.86.2014.07.22.16.57.35 for (version=SSLv3 cipher=RC4-SHA bits=128/128); Tue, 22 Jul 2014 16:57:35 -0700 (PDT) In-Reply-To: <20140722142517.3a7bee43@voltron.arsc.edu> X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.85.220.42 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:98914 Archived-At: On Tue, 22 Jul 2014 14:25:17 -0800 Christopher Howard 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