Question

I'm trying to write strings with '®' to a csv file:

csvfile  = open(destination, "wb")
csv_writer = csv.writer(csvfile, quoting=csv.QUOTE_MINIMAL, delimiter='\t')
for row in data:
    csv_writer.writerow(row)

csvfile.close()

and row looks like this:

[123, "str", "str2 ®"]

The strings I'm trying to write to csv is retrieved from xml, which I believe is encoded to utf-8.

I get error:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/ec2-user/django/app/models.py", line 94, in import_data
    load_to_csv(out, out_data)
  File "/home/ec2-user/django/utils/util.py", line 90, in load_to_csv
    csv_writer.writerow(row)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xae' in position 66: ordinal not in range(128)

Then I tried to encode the string to utf-8:

csvfile  = open(destination, "wb")
csv_writer = csv.writer(csvfile, quoting=csv.QUOTE_MINIMAL, delimiter='\t')
for row in data:
    for i, r in enumerate(row):
        if type(r) is str:
            row[i] = r.encode('utf-8')
    csv_writer.writerow(row)

csvfile.close()

But I still get the same error.. Could anyone help? Have been stuck for a while..

Was it helpful?

Solution

You have a Unicode value, not a byte string. Encode those:

for row in data:
    row = [c.encode('utf8') if isinstance(c, unicode) else c for c in row]:
    csv_writer.writerow(row)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top