UnicodeDecodeError: 'gbk' codec can't decode byte 0xa0 in position 18: illegal multibyte sequence
时间: 2024-06-22 07:03:01 浏览: 302
basemap readshapefile UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb4 in position 0-附件资源
当遇到`UnicodeDecodeError: 'gbk' codec can't decode byte 0xa0 in position 18: illegal multibyte sequence`错误时,说明你在尝试使用GBK编码读取一个实际使用UTF-8编码的文件。为了解决这个问题,你需要确保文件的编码与你读取文件时指定的编码一致。在这个例子中,你需要将`open(filename, 'r', encoding='UTF-8')`中的`encoding='UTF-8'`改为`encoding='gbk'`,如果文件是用GBK编码的。
修改后的代码如下:
```python
def cfg_from_file(filename):
"""Load a config file and merge it into the default options."""
import yaml
with open(filename, 'r', encoding='gbk') as f: # 将UTF-8改为gbk
yaml_cfg = edict(yaml.full_load(f))
_merge_a_into_b(yaml_cfg, __C)
```
阅读全文