‘gbk‘ codec can‘t decode byte 0xa4 in position 4: illegal multibyte sequence
原因:
在用python读取文件,尤其是中文文件的时候会出现这种错误,错误里提示gbk国标的编码不能进行解码,所以可以判断这里是读取器文件是的编码方式无法识别。
解决:
将源码读取文件的地方添加参数`encoding="utf-8"就行了
f = open('sc.txt',encoding="utf-8")
lines = f.readlines()
# print(lines)
f.close()results = []for line in lines:print (line)data = line.split()print (data)sum = 0score_list = data[1:]for score in score_list:sum += int(score)result = '%s \t: %d\n' % (data[0], sum)print (result)results.append(result)print (results)
output = open('result.txt', 'w', encoding='gbk')
output.writelines(results)
output.close()
sc.txt
张三 23 35 44 47 51
李四 60 77 68
王小五 97 99 89 85
周六 100
总结:
with open(“./result.txt”, “r”,) as f: # 打开文件
data = f.read() # 读取文件
报错
UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xa4 in position 4: illegal multibyte sequence
解决:加入编码——encoding=‘UTF-8’
with open("./result.txt", "r",encoding='UTF-8') as f: # 打开文件data = f.read() # 读取文件
‘gbk‘ codec can‘t decode byte 0xa4 in position 4: illegal multibyte sequence相关推荐
- ‘gbk‘ codec can‘t decode byte 0xa4 in position 16433: illegal multibyte sequence
在读取gbk格式的数据中包含有其他的特殊字符出现以下报错信息 'gbk' codec can't decode byte 0xa4 in position 16433: illegal multiby ...
- UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0x80 in position 658: illegal multibyte sequence
解决Python报错–UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 658: illegal multibyte ...
- 成功解决UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xba in position 2: illegal multibyte sequence
成功解决UnicodeDecodeError: 'gbk' codec can't decode byte 0xba in position 2: illegal multibyte sequence ...
- UnicodeDecodeError: 'gbk' codec can't decode byte 0xab in position 43: illegal multibyte sequence
python读取txt文件时报错: UnicodeDecodeError: 'gbk' codec can't decode byte 0xab in position 43: illegal mul ...
- UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 199: illegal multibyte sequence
在做<机器学习实战>里的朴素贝叶斯算法时提示错误 UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 19 ...
- UnicodeDecodeError: 'gbk' codec can't decode byte 0xd2 in position 85: illegal multibyte sequence
1.今天,写一个小代码运行时,报了这个错误:UnicodeDecodeError: 'gbk' codec can't decode byte 0xd2 in position 85: illegal ...
- UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xae in position 16: illegal multibyte sequence
[问题描述] 今天在使用scrapy框架的时候出现了如下错误: Traceback (most recent call last):File "d:\anaconda\lib\runpy.p ...
- Python 基于csv 读取文本文件提示:‘gbk‘ codec can‘t decode byte 0xbf in position 2: illegal multibyte sequence
报错 Python 基于csv 读取文本文件提示:'gbk' codec can't decode byte 0xbf in position 2: illegal multibyte sequenc ...
- UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 167: illegal multibyte sequence
UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 167: illegal multibyte sequence 文 ...
最新文章
- Linux Shell 命令--grep
- python判断数据类型type_Python 判断数据类型有type和isinstance
- 汇编中16进制的写法问题
- 今夕何夕影迷小伙伴,喜欢的壁纸都在高图网
- linq to xml查询绑定GridView
- gflags.lib(gflags.obj) : error LNK2001: 无法解析的外部符号 __imp_PathMatchSpecA
- Quill – 可以灵活自定义的开源的富文本编辑器
- bzoj 2795	[Poi2012]A Horrible Poem hash+线性筛
- 属性篇(4)—If you love css …
- 软件测试方法和技术知识点有哪些?
- Macbook用Terminal启动Matlab
- tsp 近似算法 matlab,TSP问题—近似算法
- debug and releas 不显示 调试窗口(DOS窗口/控制台)
- Excel图表制作(二):滚动条实现动态图表
- 顺丰快递查询接口的调用方法(附带源码)
- 打开Windows任务管理器的七种方法
- 七牛云彭垚:智能平台的创新和发展
- Latex排版—(2)基本结构
- DICOM:再次剖析fo-dicom中DicomService的自定义事件绑定
- 原生 js 实现无缝滚动轮播