# Time: 2020/05/31
#Author: Xiaohong
# 运行环境: OS: Windows 10
# Python: 3.7
# 功能: 读文件时,产生错误的解决方法
# 错误提示:
'utf-8' codec can't decode byte 0xb5 in position 0: invalid start byte
或:
'gbk' codec can't decode byte 0xfe in position 16172: illegal multibyte sequence
原因为: open 文件时,指定的 encoding 类型,在文本文件中出现 超出改类型的文字,故报错
具体解释为:中文字符的Unicode编码0x0800-0xFFFF之间,(utf-8包含了部分汉字)。当你试图将该“中文字符”转成U码的utf-8时超出了其范筹。而GBK 规范收录了 ISO 10646.1 中的全部 CJK 汉字和符号,并有所补充,
解决方法: encoding 的类型做下拓展,也可以指定忽略(加上 errors='ignore'),如:
fr_xyj=open('E:\\00000\\xyj.txt','r',encoding='gbk')
或:
fr_xyj=open('E:\\00000\\xyj.txt','r',encoding='GB18030')
或:
fr_xyj=open('E:\\00000\\xyj.txt','r',encoding='utf-8', errors='ignore')
附:字符编码详解
https://www.crifan/files/doc/docbook/char_encoding/release/html/char_encoding.html
更多推荐
Python 读文件错误: invalid start byte / illegal multibyte sequence
发布评论