UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbd in position 31: invalid start byte
时间: 2023-10-16 07:10:11 浏览: 292
basemap readshapefile UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb4 in position 0-附件资源
这个错误的原因是当尝试将一个字节流解码成utf-8编码时,遇到了一个无效的起始字节。
解决这个问题的方法是将.decode('utf-8')改为.decode('gbk'),因为GBK规范收录了ISO 10646.1中的全部CJK汉字和符号,并有所补充。
在代码中,你可以使用以下代码将文件以gbk编码方式打开:
```python
fp = open('data.txt','r ',encoding='gbk')
```
然后将数据读取并处理。
这样就可以避免UnicodeDecodeError错误的出现。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd5 in position 2: invalid continuation byte-...](https://download.csdn.net/download/weixin_42204453/15589184)[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_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xb4...](https://blog.csdn.net/Scarlett2025/article/details/117446536)[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_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [UnicodeDecodeError: utf-8 codec can t decode byte 0x8b in position 1: invalid start byte](https://blog.csdn.net/m0_52613986/article/details/129739448)[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_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文