MarkJ
MarkJ

Reputation: 411

Unicode Error on Python

So I'm trying to make a table with a csv table that has unicode characters in it:

with open('test1.csv', 'w') as csvfile:
    writer = csv.writer(csvfile)
    [writer.writerow(r) for r in table]

I get this error every time i try to run my program, though:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 8-10: ordinal not in range(128)

How do I fix this?

Upvotes: 2

Views: 4888

Answers (2)

ekhumoro
ekhumoro

Reputation: 120818

Assuming you're using Python2:

with open('test1.csv', 'wb') as csvfile:
    writer = csv.writer(csvfile)
    for r in table:
        writer.writerow([x.encode('utf-8') for x in r])

Of course, when you open the csv file, you will also need to decode it using the same encoding:

with open('test1.csv') as csvfile:
    reader = csv.reader(csvfile.decode('utf-8'))

(NB: if you used Python3, none of this would be necessary - your original example would work fine as it is).

Upvotes: 2

Kasravnd
Kasravnd

Reputation: 107357

First of all you don't need to use a list comprehension for write in csv file, secondly if you are using python 2.X you can use codecs module to open your file with a proper encoding and if you are using python 3.X you can use encoding argument in open function.

Also note that since the write method used your default encoding if still you got unicode error you can use str.encode() method in write method.

Python 2.X:

import codecs
with codecs.open(filename, 'w', encoding='utf-8') as csvfile:
     writer = csv.writer(csvfile)
     for r in table:
         writer.writerow(r.encode('utf-8'))

Python 3.X :

with open(filename, 'wb', encoding='utf-8') as csvfile:
     writer = csv.writer(csvfile)
     for r in table:
         writer.writerow(r.encode('utf-8'))

Upvotes: 2

Related Questions