2

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?

2
  • 2
    What version of Python are you using? If python 3, did you try openning the file in the enoding in which your row is with encoding='<encoding>' argument to open() ? Commented Sep 30, 2015 at 15:31
  • possible duplicate of Python: Convert Unicode to ASCII without errors for CSV file Commented Sep 30, 2015 at 15:31

2 Answers 2

2

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).

Sign up to request clarification or add additional context in comments.

Comments

2

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'))

4 Comments

I get this error when I use your code: UnicodeEncodeError: 'ascii' codec can't encode characters in position 8-10: ordinal not in range(128)
I'm getting this error now: AttributeError: 'list' object has no attribute 'encode'
@K.B Is your table contains lists?
Yeah, the element table is full of lists

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.