UnicodeDecodeError: 'ascii' codec can't decode byte 0x8b in position 6: ordinal not in range(128)
时间: 2023-10-18 20:00:02 浏览: 142
这个错误是由于Python默认的编码是ascii,但是当程序中遇到非ascii编码时会报错。解决这个问题的方法有两种。
第一种方法是在程序中加入以下代码来设置默认编码为utf8:
```python
# encoding=utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')
```
第二种方法是创建一个名为sitecustomize.py的文件,并将其放在Python的Lib\site-packages文件夹下,内容为:
```python
# encoding=utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')
```
这样,在每次重启Python解释器后,系统会自动调用该文件来设置默认编码为utf8,避免了每次手动添加代码的麻烦。
请注意,修改编码只在当前解释器会话中有效,重启解释器后会恢复默认的ascii编码。以上方法可以实现一劳永逸的解决方案。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [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: 33.333333333333336%"]
- *3* [python2(中文编码问题):UnicodeDecodeError: 'ascii' codec can't decode byte 0x?? in position 1...](https://blog.csdn.net/weixin_30951743/article/details/97360468)[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: 33.333333333333336%"]
[ .reference_list ]
阅读全文