UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 45: illegal multibyte sequence、
时间: 2023-10-22 20:28:21 浏览: 90
当你使用pyquery库读取本地HTML文件时,出现"UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 45: illegal multibyte sequence"的错误。这个错误通常是因为文件的编码方式与你的python编码方式不匹配所致。
解决这个问题的方法是根据文件的实际编码方式来设置python的编码方式。如果文件是utf-8编码,你可以尝试使用以下代码来打开文件:
```python
with open(f_path, 'r', encoding='utf-8') as f:
```
这样,你就可以使用utf-8编码来读取文件了。
如果你的文件依然报错,你可以尝试忽略错误,这样即使遇到编码问题也不会中断程序的执行:
```python
with open(f_path, 'r', encoding='utf-8', errors='ignore') as f:
```
这样,即使文件中存在无法解码的字符,程序也会继续执行而不会报错。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [pyquery报错:UnicodeDecodeError: ‘gbk’ codec can’t decode byte](https://download.csdn.net/download/weixin_38665944/13751270)[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* *3* [UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xad in position 110: illegal multibyte ...](https://blog.csdn.net/qq_16792139/article/details/124152710)[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 ]
阅读全文
相关推荐


















