CodexBloom - Programming Q&A Platform

Python 2.7: Handling UnicodeEncodeError when writing to CSV with csv.writer

👀 Views: 20 💬 Answers: 1 📅 Created: 2025-08-22
python-2.7 csv unicode encoding Python

I'm prototyping a solution and I recently switched to I'm having a hard time understanding I'm working with a `UnicodeEncodeError` when trying to write a list of dictionaries containing Unicode characters to a CSV file using the `csv` module in Python 2.7. The data contains some non-ASCII characters, and when I attempt to write it using the following code: ```python import csv data = [ {u'name': u'Jürgen', u'age': 30}, {u'name': u'Sofia', u'age': 25}, {u'name': u'François', u'age': 22} ] with open('output.csv', 'wb') as csvfile: fieldnames = ['name', 'age'] writer = csv.DictWriter(csvfile, fieldnames=fieldnames) writer.writeheader() for row in data: writer.writerow(row) ``` I receive the following behavior: ``` UnicodeEncodeError: 'ascii' codec need to encode character u'\xfc' in position 0: ordinal not in range(128) ``` I've tried setting the locale with `import locale` and `locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')`, but it didn't help. Additionally, I've attempted to encode the strings before writing them by modifying the `writer.writerow(row)` line like this: ```python writer.writerow({k: v.encode('utf-8') if isinstance(v, unicode) else v for k, v in row.items()}) ``` However, that didn't resolve the scenario either. I'm currently using Python 2.7.18 and would appreciate any advice on how to correctly handle the Unicode characters when writing to a CSV file. What am I missing? Hoping someone can shed some light on this. Any pointers in the right direction? This is part of a larger service I'm building. Thanks, I really appreciate it!