[Tutor] Urgent: unicode problems writing CSV file

Peter Otten __peter__ at web.de
Wed Jun 8 14:07:28 EDT 2016


Alex Hall wrote:

> The type  of the 'info' variable can vary, as I'm pulling it from a
> database with Pyodbc. I eventually found something that works, though I'm
> not fully sure why or how.

As Tim says, the csv.writer in Python 2 applies str() to every value.

If that value is a unicode instance this results in

value.encode(sys.getdefaultencoding())

In every sanely configured Python installation the default encoding is 
"ascii", hence the UnicodeEncodeError. If you manually encode for unicode 
objects (with an encoding that can encode all its codepoints)

> csvWriter.writerow([info.encode("utf8") if type(info)is unicode else info
> for info in resultInfo])

the str(value) that follows sees a byte string, becomes a noop and will 
never fail.

> where resultInfo is an array holding the values from a row of database
> query results, in the order I want them in.
 




More information about the Tutor mailing list