Question on the "csv" library

John Machin sjmachin at lexicon.net
Fri Aug 28 15:00:36 EDT 2009


On Aug 29, 2:35 am, vsoler <vicente.so... at gmail.com> wrote:

> 3- Excel does not even put quotes around litteral texts, not even when
> the text contains a blank

Correct. Quoting is necessary only if a text field contains a
delimiter (semicolon/comma), a newline, or the quote character.

You can read Excel CSV output using the Python csv module by
specifying delimiter=";"

If you need to feed a CSV file to some software that demands that text
fields be quoted unconditionally, you have at least two options:

(1) If you know which fields should be text fields, you can read the
Excel-output file with Python csv, convert your non-text fields to
float, and write it back out with quoting=csv.QUOTE_NONNUMERIC.

(2) If you want precise control (and thus precise knowledge), use xlrd
(http://pypi.python.org/pypi/xlrd) to read Excel 97-2003 .xls files --
it will tell you cell by cell (NOT column by column (the user has
control of the type at the cell level)) whether the cell contains text
(reported as a unicode object), a number (float), a Boolean (int, 1 or
0), a date (float, days since ? (read the docs)), or an error code
e.g. #DIV/0! (int, read the docs) ... then you can write it out
however you like.

HTH,
John



More information about the Python-list mailing list