close

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

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error UnicodeDecode: (‘utf-8’ codec) while reading a csv file in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How UnicodeDecode: (‘utf-8’ codec) while reading a csv file Error Occurs?

Today I get the following error UnicodeDecode: (‘utf-8’ codec) while reading a csv file in python.

How To Solve UnicodeDecode: (‘utf-8’ codec) while reading a csv file Error ?

  1. How To Solve UnicodeDecode: ('utf-8' codec) while reading a csv file Error ?

    To Solve UnicodeDecode: ('utf-8' codec) while reading a csv file Error One simple solution is you can open the csv file in an editor like Sublime Text and save it with 'utf-8' encoding. Then we can easily read the file through pandas.

  2. UnicodeDecode: ('utf-8' codec) while reading a csv file

    To Solve UnicodeDecode: ('utf-8' codec) while reading a csv file Error One simple solution is you can open the csv file in an editor like Sublime Text and save it with 'utf-8' encoding. Then we can easily read the file through pandas.

Solution 1

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

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

Solution 2

One simple solution is you can open the csv file in an editor like Sublime Text and save it with ‘utf-8’ encoding. Then we can easily read the file through pandas.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read