UnicodeDecodeError:“utf-8”编解码器无法解码位置 15 中的字节 0x96:起始字节无效

har*_*han 4 csv python-3.x pandas

import csv
import pandas as pd
db = input("Enter the dataset name:")
table = db+".csv"
df = pd.read_csv(table)
df = df.sample(frac=1).reset_index(drop=True)
with open(table,'rb') as f:
    data = csv.reader(f)
    for row in data:
        rows = row
        break
print(rows)
Run Code Online (Sandbox Code Playgroud)

我正在尝试读取 csv 文件中的所有列。

错误:UnicodeDecodeError:“utf-8”编解码器无法解码位置 15 中的字节 0x96:起始字节无效

sha*_*eed 5

您需要检查csv文件的编码。

为此,您可以使用print(f)

with open('file_name.csv') as f:
    print(f)
Run Code Online (Sandbox Code Playgroud)

输出将是:

<_io.TextIOWrapper name='file_name.csv' mode='r' encoding='utf8'>
Run Code Online (Sandbox Code Playgroud)

csv使用上面输出中提到的编码打开,

with open(fname, "rt", encoding="utf8") as f:
Run Code Online (Sandbox Code Playgroud)

正如评论中提到的,您的编码是cp1252

所以,

with open(fname, "rt", encoding="cp1252") as f:
    ...
Run Code Online (Sandbox Code Playgroud)

并且对于.read_csv,

df = pd.read_csv(table, encoding='cp1252')
Run Code Online (Sandbox Code Playgroud)