简体   繁体   English

使用 utf-8 编码和解码返回 UnicodeError

[英]Encoding and decoding with utf-8 returns UnicodeError

I am both enconding and decoding with utf-8 but still I get a UnicodeError.我正在使用 utf-8 进行编码和解码,但仍然出现 UnicodeError。

import pandas as pd
df.to_csv('myfile.csv', index=False, encoding='utf-8')

Then, in another.py, same project然后,在另一个.py 中,同一个项目

import pandas as pd
with open(file, 'r') as f:
    csv = pd.read_csv(f, encoding='utf-8')

The error is: UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 51956: character maps to <undefined> This is not the first time I get this issue.错误是: UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 51956: character maps to <undefined>这不是我第一次遇到这个问题。

Ok, found it.好的,找到了。 Makes a lot of sense now.现在很有意义。

with open(file, 'r', encoding='utf-8') as f:
    csv = pd.read_csv(f)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM