Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (19.9k points)

I'm trying to write a to a csv file, I get the following error:

UnicodeEncodeError: 'ascii' codec can't encode character u'\u03b1' in position 20: ordinal not in range(128)

Line causing error:


1 Answer

0 votes
by (25.1k points)

As the error message suggests the error is being caused because of encoding. Now this could simply be an error because of encoding or it could be because the separator is incorrect.

In csv files all values are separated by commas, but that's not always the case. Sometime people also use other separator like tabs etc.

Also for a computer to be able to understand text written in a file, it is written in a special computer understandable code this code. This special code used to be ASCII but now is unicode used most predominantly (utf-8, utf-16 etc).

So either the encoding is incorrect or the separators are incorrect. 

You can resolve this error by specifying these details in the to_csv methoid like this:

df.to_csv(file_name, sep='\t', encoding='utf-8')

You can learn more about panads using this tutorial:

Browse Categories