I want to output a list of dictionaries to a CSV file.
I have the following code :
# -*- coding: utf-8 -*-
import csv
items = [{'ởne': 1, 'twở': 2}, {'ởne': 1, 'twở': 2}]
# Note the usage of the "ở" character, aka "\u1edf"
with open('output.csv', 'w') as f:
fieldnames = items[0].keys() # fetching the keys of the first item as fieldnames
w = csv.DictWriter(f, fieldnames=fieldnames, delimiter=';', lineterminator='\n')
w.writeheader()
w.writerows(items)
This fails with the following error :
UnicodeEncodeError: 'charmap' codec can't encode character '\u1edf' in position 0: character maps to
How can I get my items to properly output to CSV ?