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 14:25:17 -0800 Message-ID: <20140722142517.3a7bee43@voltron.arsc.edu> References: <20140722105221.254307f2@voltron.arsc.edu> <87oawhchko.fsf@gmail.com> 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 1406067864 30556 80.91.229.3 (22 Jul 2014 22:24:24 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 22 Jul 2014 22:24:24 +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 00:24:17 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 1X9iTo-0003Q6-Ak for geh-help-gnu-emacs@m.gmane.org; Wed, 23 Jul 2014 00:24:16 +0200 Original-Received: from localhost ([::1]:41641 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X9iTn-00064I-Iw for geh-help-gnu-emacs@m.gmane.org; Tue, 22 Jul 2014 18:24:15 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:33300) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X9iTV-000641-DX for help-gnu-emacs@gnu.org; Tue, 22 Jul 2014 18:24:04 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1X9iTP-0002Mk-6F for help-gnu-emacs@gnu.org; Tue, 22 Jul 2014 18:23:57 -0400 Original-Received: from mail-pa0-f43.google.com ([209.85.220.43]:41456) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X9iTP-0002Me-0u for help-gnu-emacs@gnu.org; Tue, 22 Jul 2014 18:23:51 -0400 Original-Received: by mail-pa0-f43.google.com with SMTP id lf10so388643pab.16 for ; Tue, 22 Jul 2014 15:23:50 -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=jyslp4+umLxVyNqXBhGmlNpRbMUli334buuDJWvjvmg=; b=NjNX9WhXCXXPTbleMUJ6tKslcFRBkkK6wugkqvlxfgfM2mkygkLUnUDrMmaKsiY/KF g5tdzhp+t45xkvUay/tDWwQNE8UQ7VdrkSRz4rBG3xqqlyjVbsDeyOjTx4frXblIfjTz YEs+o5lrK6RZD/kBzQ8lGL+e0QguTy2NHZZ0GsObR8vfII2EaNlZ5iwL93DswQi21tkg NeT1kQOFMr7yn5tslVORU2ciQTKtozf8mnVy3y7HzNQ7dduTva13om0M/LQOrtTP6klR RhRgQjIgaRVMZgyBlll1mp+9LlWQ6lrXA3Wan0pv1guxEvm65kcHar8K6bUF44jyDRXX FhrQ== X-Gm-Message-State: ALoCoQm8T7h/UC2XwFPyvRKeN5HAFRtPMKg+1o7oLXst0ArV0IU5q7uR5570ioGz+S5/zN1/R1Wh X-Received: by 10.70.65.100 with SMTP id w4mr5517718pds.128.1406067829900; Tue, 22 Jul 2014 15:23:49 -0700 (PDT) Original-Received: from voltron.arsc.edu ([199.165.81.33]) by mx.google.com with ESMTPSA id bz4sm256416pbd.25.2014.07.22.15.23.48 for (version=SSLv3 cipher=RC4-SHA bits=128/128); Tue, 22 Jul 2014 15:23:49 -0700 (PDT) In-Reply-To: <87oawhchko.fsf@gmail.com> 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.43 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:98912 Archived-At: On Tue, 22 Jul 2014 21:35:35 +0200 Thorsten Jolitz wrote: > Christopher Howard 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