csv and mixed lists of unicode and numbers
mal at egenix.com
Wed Nov 25 17:53:16 CET 2009
Sibylle Koczian wrote:
> I want to put data from a database into a tab separated text file. This
> looks like a typical application for the csv module, but there is a
> snag: the rows I get from the database module (kinterbasdb in this case)
> contain unicode objects and numbers. And of course the unicode objects
> contain lots of non-ascii characters.
> If I try to use csv.writer as is, I get UnicodeEncodeErrors. If I use
> the UnicodeWriter from the module documentation, I get TypeErrors with
> the numbers. (I'm using Python 2.6 - upgrading to 3.1 on this machine
> would cause other complications.)
> So do I have to process the rows myself and treat numbers and text
> fields differently? Or what's the best way?
It's best to convert all data to plain strings before passing it
to the csv module.
There are many situations where you may want to use a different
string format than the standard str(obj) format, so this is best
done with a set of format methods - one for each type and format
you need, e.g. one for integers, floats, monetary values, Unicode
text, plain text, etc.
The required formatting also depends on the consumers of the
generated csv or tsv file and their locale.
Professional Python Services directly from the Source (#1, Nov 25 2009)
>>> Python/Zope Consulting and Support ... http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
::: Try our new mxODBC.Connect Python Database Interface for free ! ::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
More information about the Python-list