我正在尝试在 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 的一个错误,还是有更好的方法来解决这个问题?