- I have a list of dictionaries containing unicode strings.
csv.DictWritercan write a list of dictionaries into a CSV file.- I want the CSV file to be encoded in UTF8.
- The
csvmodule cannot handle converting unicode strings into UTF8. -
The
csvmodule documentation has an example for converting everything to UTF8:def utf_8_encoder(unicode_csv_data): for line in unicode_csv_data: yield line.encode('utf-8') -
It also has a
UnicodeWriterclass.
But… how do I make DictWriter work with these? Wouldn’t they have to inject themselves in the middle of it, to catch the disassembled dictionaries and encode them before it writes them to the file? I don’t get it.
UPDATE: The 3rd party unicodecsv module implements this 7-year old answer for you. Example below this code. There’s also a Python 3 solution that doesn’t required a 3rd party module.
Original Python 2 Answer
If using Python 2.7 or later, use a dict comprehension to remap the dictionary to utf-8 before passing to DictWriter:
You can use this idea to update UnicodeWriter to DictUnicodeWriter:
Python 2 unicodecsv Example:
Python 3:
Additionally, Python 3’s built-in csv module supports Unicode natively: