gpt4 book ai didi

python - 如何解决 UnicodeDecodeError : 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte in python

转载 作者:行者123 更新时间:2023-12-04 12:18:24 24 4
gpt4 key购买 nike

我潦草地写下数据,不得不将数据帧保存为 utf-16 (Unicode),因为拉丁语/西类牙语单词以 utf-8 的形式显示很奇怪。我使用以下代码来保存数据帧:

 df.to_csv("blogdata.csv", encoding = "utf-16", sep = "\t", index = False)

当我尝试使用以下代码读取文件以清理数据时:
 blogdata = pd.read_csv('c:/Users/hyoungm?Downloads/blogdata.csv')

它显示以下错误。

UnicodeDecodeError 回溯(最近一次调用最后一次)
在 ()
----> 1 blogdata = pd.read_csv('C:/Users/hyoungm/Downloads/blogdata.csv')

...

pandas._libs.parsers.TextReader 中的 pandas_libs\parsers.pyx。 cinit ()

pandas._libs.parsers.TextReader._get_header() 中的 pandas_libs\parsers.pyx

UnicodeDecodeError: 'utf-8' 编解码器无法解码位置 0 中的字节 0xff:起始字节无效

请在此处查看我的屏幕截图:
enter image description here

我不知道如何在不丢失英文句子中的拉丁文/西类牙文单词的情况下保存原始数据,也不知道如何读取 Unicode 数据文件。有人可以帮我解决这个问题吗?

非常感谢!

最佳答案

当编码未知时,有一个 Python 库可能会有所帮助:chardet

with open(filename, 'rb') as file:
print(chardet.detect(file.read()))

检测找到编码,和
'rb' 将以二进制形式读取文件

关于python - 如何解决 UnicodeDecodeError : 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte in python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55563399/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com