python pickle load UnicodeDecodeError

UnicodeDecodeError: 'rawunicodeescape' codec can't decode bytes in position 207-211: truncated \uXXXX

代码:

file2 = open("message_list.pkl")
subject_list = pickle.load(file2)



修改为


file2 = open("message_list.pkl","rb")



啦啦啦~~~~~~~~~~~~~~~~~~~~~可以正常运行了

回答: 这个错误是由于Python默认使用的编码方式是ASCII,而在你的代码中遇到了非ASCII字符导致的。解决这个问题的方法有几种。一种方法是在文件开头添加以下代码来指定文件的编码方式为UTF-8: ```python # -*- coding: utf-8 -*- ``` 另一种方法是在打开文件时指定编码方式为UTF-8: ```python with open(file, 'rb') as f: data = f.read().decode('utf-8') ``` 你也可以尝试使用其他编码方式来解决这个问题,比如使用'latin-1'编码方式。希望这些方法能够帮助你解决问题。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* [UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0xe7 in position 0: ordinal not in range(128)...](https://blog.csdn.net/qq_43874102/article/details/124301315)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [已解决UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0x8e in position 0: ordinal not in ...](https://blog.csdn.net/yuan2019035055/article/details/125752401)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值