UnicodeDecodeError:'utf-8'编解码器无法解码位置15的字节0x96:无效的起始字节 [英] UnicodeDecodeError: 'utf-8' codec can't decode byte 0x96 in position 15: invalid start byte

查看:235
本文介绍了UnicodeDecodeError:'utf-8'编解码器无法解码位置15的字节0x96:无效的起始字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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)

我正在尝试从csv文件中读取所有列.

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

ERROR: UnicodeDecodeError: 'utf-8' codec can't decode byte 0x96 in position 15: invalid start byte

推荐答案

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

You need to check encoding of your csv file.

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

For that you can use print(f) like this,

with open('file_name.csv') as f:
    print(f)

输出是这样的:

<_io.TextIOWrapper name='file_name.csv' mode='r' encoding='utf8'>

以这种编码打开csv

with open(fname, "rt", encoding="utf8") as f:

如评论中所述, 您的编码是cp1252

As mentioned in comments, your encoding is cp1252

所以

with open(fname, "rt", encoding="cp1252") as f:
    ...

,对于.read_csv

df = pd.read_csv(table, encoding='cp1252')

这篇关于UnicodeDecodeError:'utf-8'编解码器无法解码位置15的字节0x96:无效的起始字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆