[sf-perl] writing a csv file: how do i embed a newline in a cell?

David Alban extasia at extasia.org
Fri Apr 8 17:44:45 PDT 2011


greetings,

i'm writing a program that will produce a csv file so that folks can
import the output into an ms excel spreadsheet.  i want to be able to
write newlines that show up inside individual cells.  i created a new
spreadsheet with a single cell, with newlines inserted inside the cell
using alt-enter, so that it looked like the following, and saved it as
a csv file.

a
b
c
d

using cygwin, i see:

$ cat junk.csv
"a
b
c
d"

$ od -c junk.csv
0000000   "   a  \n   b  \n   c  \n   d   "  \r  \n
0000013

oh, ok, i need to quote the cell and use newlines.  great.  but when i
import that back into excel, specifying delimited text and accepting
the tab character as delimiter, i get four cells one in the same
column, the last cell containing the trailing double quote:

a
b
c
d"

wtf?

i also tried using perl to create the file with only newlines,
newlines and carriage returns, and only carriage returns, double
quoting what should be in a single cell.  nothing i  tried resulted in
a newline inside a cell.  any ideas?  the data will all be simple text
with the exception that i'd like to insert newlines inside of cells.

thanks,
david
-- 
Live in a world of your own, but always welcome visitors.


More information about the SanFrancisco-pm mailing list