Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
403 views
in Technique[技术] by (71.8m points)

python 3.x - Importing csv using pd.read_csv - invalid start byte error

I'm trying to import a csv file using:

data = pd.read_csv("filename.csv")

I get the following error: "UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb0 in position 2: invalid start byte".

The answer in this question: UnicodeDecodeError: 'utf8' codec can't decode byte 0x9c might work, but I am not sure how to implement it (I can't comment on the answer because I don't have enough reputation yet).

Any help would be appreciated.

Edit: The issue seems to be linked to the fact that I have a degree symbol. It would be fine for me if during import this issue is just skipped.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you face an encoding error due to encoding on your file not being the default as mentioned by the pd.read_csv() docs , you can find the encoding of the file by first installing chardet followed by the below code:

import chardet    
rawdata = open('D:\path\file.csv', 'rb').read()
result = chardet.detect(rawdata)
charenc = result['encoding']
print(charenc)

This will give you the encoding of the file.

Once you have the encoding, you can read as :

pd.read_csv('D:\path\file.csv',encoding = 'encoding you found')

or

pd.read_csv(r'D:pathfile.csv',encoding = 'encoding you found')

You will get the list of all encoding here

Hope you find this useful.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...