UnicodeDecodeError gbk codec can't decode byte in position illegal multibyte sequence
程序员文章站
2022-03-31 19:14:57
...
UnicodeDecodeError:‘gbk’ codec can’t decode byte in position : illegal multibyte sequence
觉得有用的话,欢迎一起讨论相互学习~Follow Me
- 为解码时发生了错误,本代码原本是用来读取使用"utf-8"编码的txt文本中的数据的,但是由于解码时被程序使用"gbk"的方式进行解码
解决方案
- 将
原始open函数
添加上encoding
参数,表示解码的方案.例如,此时我使用"uft-8"的方式解码原始以"utf-8"方式编码的文件. - 即
encoding="utf-8"
# 原始代码
# with open(file='./text.txt', mode='r') as fp:
# 修改后代码
with open(file='./text.txt', mode='r', encoding='utf-8') as fp:
推荐阅读
-
Anaconda中启动Python时的错误:UnicodeDecodeError: 'gbk' codec can't decode byte 0xaf in position 553
-
UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0xcb in position 260: ordinal not in range(128)
-
命令行调用python出现编码错误:UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0x9a in position 140 完美解决!
-
Python2.7更新pip:UnicodeDecodeError: 'ascii' codec can't decode byte 0xb7 in position 7: ordinal not in range(128)
-
真正解决Windows下UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xff in position 0错误的方法
-
UnicodeDecodeError: 'utf-8' codec can't decode byte in position : invalid continuation byte
-
【debug】UnicodeDecodeError: codec can't decode byte 0xbd in position 4: invalid start byte
-
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc7 in position 0: invalid continuation byte
-
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd6 in position 0: invalid continuation byte
-
【python】pip安装报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xce in position 7: ordinal not i