Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

writing Unicode to CSV with Python

Hi, I have a list in unicode, and I want to write it into CSV with python CSV module. Example below:

var= [u'e\xe1e', u'pru\xe7ba', 30000.0]

I get the following error: UnicodeEncodeError:

'ascii' codec can't encode character

I understand that writing unicode into CSV is tricky and the video refers to unicodecsv module for python: https://pypi.python.org/pypi/unicodecsv/0.14.1

I am unable to understand how to use that module and be able to save my unicode into csv.

Please help!

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

To write Unicode to a file, the file needs to be opened with the correct encoding specified, such as encoding=utf-8.

More info and example in the Python docs on reading and writing unicode data

Post back if you need more help. Good luck!!!