python csv 'gbk' codec can't decode byte 0x81 in position 1329: illegal multibyte sequence
时间: 2023-08-14 16:04:05 浏览: 166
这个错误通常出现在尝试读取或解码包含非标准字符的文件时。对于每一个报错,都有一个"gbk"编解码器无法解码一个非法的多字节序列,导致UnicodeDecodeError的错误。
对于第一个引用,报错是UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0x80 in position 2467: illegal multibyte sequence。这个错误的解决方法是将文件的编码方式改为utf-8。可以尝试使用如下代码来打开和读取文件:
```python
with open('filename', 'r', encoding='utf-8') as f:
# 处理文件内容
```
对于第二个引用,报错是'gbk' codec can't decode byte 0x9f in position 26: illegal multibyte sequence。同样的,解决方法是将文件的编码方式改为utf-8。可以尝试使用如下代码来读取json文件:
```python
import json
with open('filename', 'r', encoding='utf-8') as f:
data = json.load(f)
```
对于第三个引用,报错是UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 198: illegal multibyte sequence。同样地,解决方法是将文件的编码方式改为utf-8。可以尝试使用如下代码来读取yaml文件:
```python
import yaml
with open('filename', 'r', encoding='utf-8') as f:
data = yaml.load(f, Loader=yaml.FullLoader)
```
最后,对于你提到的报错"python csv 'gbk' codec can't decode byte 0x81 in position 1329: illegal multibyte sequence",同样的解决方法是将文件的编码方式改为utf-8。可以尝试使用如下代码来读取csv文件:
```python
import csv
with open('filename', 'r', encoding='utf-8-sig') as f:
reader = csv.reader(f)
for row in reader:
# 处理每一行数据
```
通过将文件的编码方式改为utf-8,可以解决这些报错并成功读取或解码文件中的内容。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [ ‘gbk’ codec can’t decode byte 0x80 in position 2467: illegal multibyte sequence](https://blog.csdn.net/wujiaojue/article/details/129906085)[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^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [Python读取json文件报错“‘gbk‘ codec can‘t decode byte 0x9f in position 26: illegal multibyte ...](https://blog.csdn.net/diana_jiuri1314/article/details/131168225)[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^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0x80 in position 198: illegal multibyte sequence](https://blog.csdn.net/hhhhhhhhhhwwwwwwwwww/article/details/128225609)[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^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文