python csv unicode 'ascii' codec can't encode character u'\xf6' in position 1: ordinal not in range(128)

Then I converted all integers to string,

You converted both integers and strings to byte strings. For strings this will use the default character encoding which happens to be ASCII, and this fails when you have non-ASCII characters. You want unicode instead of str.

self.writer.writerow([unicode(s).encode("utf-8") for s in row])

It might be better to convert everything to unicode before calling that method. The class is designed specifically for parsing Unicode strings. It was not designed to support other data types.


From the documentation:

  • http://docs.python.org/library/stringio.html?highlight=cstringio#cStringIO.StringIO

Unlike the StringIO module, this module is not able to accept Unicode strings that cannot be encoded as plain ASCII strings.

I.e. only 7-bit clean strings can be stored.

Tags:

Python

Csv