【发布时间】:2021-02-24 01:09:05
【问题描述】:
我正在尝试在 Colab 中打开一个使用 gb-2312 编码的文件。这是我成功在我的 IDE 中运行以读取和解码的代码:
file = open(r'file.txt')
opened = file.read()
decoded = opened.encode('latin1').decode('gb2312')
print(decoded)
当我在 colab 中运行此代码时,我收到以下错误:
'utf-8' codec can't decode byte 0xc6 in position 67: invalid continuation byte
但如果不先使用 read() 或 list() 就无法解码,否则会出现以下错误:
'_io.TextIOWrapper' object has no attribute 'encode'
这似乎是第 22 条规则。这是 Colab 的错误还是有更好的方法来解决这个问题?
【问题讨论】:
标签: utf-8 google-colaboratory gb2312