[Tutor] Urgent: unicode problems writing CSV file

Albert-Jan Roskam sjeik_appie at hotmail.com
Wed Jun 8 15:22:14 EDT 2016


> Date: Thu, 9 Jun 2016 03:57:21 +1000
> From: steve at pearwood.info
> To: tutor at python.org
> Subject: Re: [Tutor] Urgent: unicode problems writing CSV file
> 
> On Wed, Jun 08, 2016 at 01:18:11PM -0400, Alex Hall wrote:



>> 
>> csvWriter.writerow([info.encode("utf8") if type(info)is unicode else info
>> for info in resultInfo])
> 
> Let's break it up into pieces. You build a list:
> 
> [blah blah blah for info in resultInfo]
> 
> then write it to the csv file with writerow. That is straightforward.
> 
> What's in the list?
> 
> info.encode("utf8") if type(info)is unicode else info
> 
> So Python looks at each item, `info`, decides if it is Unicode or not, 
> and if it is Unicode it converts it to a byte str using encode, 
> otherwise leaves it be.
> 
> If it makes you feel better, this is almost exactly the solution I would 
> have come up with in your shoes, except I'd probably write a helper 
> function to make it a bit less mysterious:
> 
> def to_str(obj):
> if isinstance(obj, unicode):
> return obj.encode('uft-8')
> elif isinstance(obj, str):
> return obj
> else:
> & Or maybe an error?
> return repr(obj)
> 
> csvWriter.writerow([to_string(info) for info in resultInfo])
>

The docs also offer some code for working with Unicode/CSV, see the bottom of this page: 
https://docs.python.org/2/library/csv.html


 		 	   		  


More information about the Tutor mailing list