UnicodeDecodeError: (‘utf-8’ codec) while reading a csv file [duplicate]

Known encoding

If you know the encoding of the file you want to read in,
you can use

pd.read_csv('filename.txt', encoding='encoding')

These are the possible encodings:
https://docs.python.org/3/library/codecs.html#standard-encodings

Unknown encoding

If you do not know the encoding, you can try to use chardet, however this is not guaranteed to work. It is more a guess work.

import chardet
import pandas as pd

with open('filename.csv', 'rb') as f:
    result = chardet.detect(f.read())  # or readline if the file is large


pd.read_csv('filename.csv', encoding=result['encoding'])

Leave a Comment