pickle的loads报错a bytes-like object is required
时间: 2023-12-17 19:58:30 浏览: 175
当使用pickle的loads函数时,出现“a bytes-like object is required”错误是因为在打开pickle文件时使用了错误的模式。在Python 3中,使用'r'模式读取文件会返回一个字符串对象,而不是字节对象。因此,需要将模式更改为'rb',以字节模式打开文件,然后使用pickle.load函数来加载pickle文件。这样就可以解决这个错误了。 例如,你可以使用以下代码来加载pickle文件:
```python
with open('train_id.pickle', 'rb') as f:
data = pickle.load(f)
```
这样就能够成功加载pickle文件并解决报错问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [pickle模块报错TypeError: a bytes-like object is required, not 'str'](https://blog.csdn.net/littlehaes/article/details/102643890)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* [pickle.load()中的TypeError: a bytes-like object is required, not 'str'](https://blog.csdn.net/qq_40392850/article/details/89951901)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文