Python Error "UnicodeEncodeError: 'ascii' codec can't encode character u'\u03b1' in position 20: ordinal not in range(128)"
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:
df.to_csv('out.csv')
ordinal not in range(128)
Answer: As the error message “ordinal not in range(128)” 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. Sometimes people also use other separators 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 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 method like this:
df.to_csv(file_name, sep='\t', encoding='utf-8')