Phi*_*hil 1 text iso-8859-1 python-3.x
我尝试读取其中带有笑脸的聊天记录,但出现以下错误:
UnicodeDecodeError:“ charmap”编解码器无法解码位置38的字节0x9d:字符映射到
我的代码如下所示:
file_name = "chat_file.txt"
chat = open(chat_file)
chatText = chat.read() # read data
chat.close()
print(chatText)
Run Code Online (Sandbox Code Playgroud)
我可以肯定这是由于诸如以下的元素:
如何实现正确的转换格式//什么是正确的文件编码,以便python可以读取这些元素?
在未指定文本编码的情况下,切勿打开文本文件。
另外,使用with块,这些块会自动调用,.close()因此您不必这样做。
file_name = "chat_file.txt"
with open(chat_file, encoding="utf8") as chat:
chat_text = chat.read()
print(chat_text)
Run Code Online (Sandbox Code Playgroud)
iso-8859-1是旧版编码,表示它不能包含表情符号。对于表情符号,文本文件必须为Unicode。Unicode的最常见编码是UTF-8。
| 归档时间: |
|
| 查看次数: |
377 次 |
| 最近记录: |