Python 'xxx' codec can't decode byte xxx常见编码错
'xxx' codec can't decode byte xxx常见编码错误处理
by:授客 QQ:1033553122
测试环境
python 3.3.2
win7
问题描述
利用python文件io方法 open打开文件,读取文件时报错,提示类似如下错误:
'xxx' codec can't decode byte xxx in position xxxx
经过好一番摸索,才大致搞懂其中的来弄去脉,暂且不说原因吧,来看下笔者做的几个实验。
源代码文件大致如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'shouke'
def testfn():
str_dic_list = []
f = open('d:\\saofu-weixin.log.2016-11-08.log', 'r') # 代码行8
counter = 0
is_found= 0
for line in f:
……(做一些处理)
testfn()
实践探索
实验1
文件(saofu-weixin.log.2016-11-08.log,以下不再赘述)编码设置:ANSI格式编码
代码行8:f = open('d:\\saofu-weixin.log.2016-11-08.log', 'r')
运行报错:
UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 4055: illegal multibyte sequence
代码行8:f = open(''d:\\saofu-weixin.log.2016-11-08.log'', 'r',encoding='utf-8')
运行报错:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb4 in position 744: invalid start byte
实验2
文件编码设置:UTF-8 无BOM格式编码
代码行8:f = open('d:\\saofu-weixin.log.2016-11-08.log', 'r')
UnicodeDecodeError: 'gbk' codec can't decode byte 0x81 in position 756: illegal multibyte sequence
f = open('d:\\saofu-weixin.log.2016-11-08.log', 'r',encoding='utf-8')
文件编码设置:USC-2 Big Endia格式编码、USC-2 Little Endia格式编码
代码行8:f = open('d:\\saofu-weixin.log.2016-11-08.log', 'r',encoding='utf-8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfe in position 0: invalid start byte
代码行8:f = open('d:\\saofu-weixin.log.2016-11-08.log', 'r')
UnicodeDecodeError: 'gbk' codec can't decode byte 0xfe in position 0: illegal multibyte sequence
代码行8:f = open('d:\\saofu-weixin.log.2016-11-08.log', 'r')
运行报错:
UnicodeDecodeError: 'gbk' codec can't decode byte 0xbf in position 2: illegal multibyte sequence
代码行:f = open('d:\\saofu-weixin.log.2016-11-08.log', 'r',encoding='utf-8')
原因分析
1、出错了,错误类型为“UnicodeDecodeError”,大致意思是Unicode解码错误
2、具体原因是: 'xxx' codec can't decode byte xxxx in position xx,大致意思就是解码器codec用‘xxx’编码去解码位于xx位置处的xxxx字节
3、进一步细化错误为:illegal multibyte sequence(非法多字节序列) 或者invalid start byte(非法的起始字符)
综上结论
1、python对编码转换的处理:从一种编码到另一种编(暂且称为目标编码)的转换,python会先把目标按某种编码解码为Unicode编码,然后再转换为目标编码。
3、python源代码文件中的注释 # -*- coding: encoding -*- 和文件解码无关,仅针对脚本文件中在内容,比如中文字符串。
我们也可以为源代码文件指定其它不同的的编码。在“#!”行之后添加如下注释语句:
# -*- coding: encoding -*-
指定编码后,源文件中的所有东西都被视为按指定编码格式编码,而非UTF-8编码。
官方原文:By default, Python source files are treated as encoded in UTF-8. In that encoding, characters of most languages in the world can be used simultaneously in string literals, identifiers and comments — although the standard library only uses ASCII characters for identifiers, a convention that any portable code should follow. To display all these characters properly, your editor must recognize that the file is UTF-8, and it must use a font that supports all the characters in the file
It is also possible to specify a different encoding for source files. In order to do this, put one more special comment line right after the #! line to define the source file encoding:
# -*- coding: encoding -*-
With that declaration, everything in the source file will be treated as having the encoding encoding instead of UTF-8
转载于:https://www.cnblogs.com/shouke/p/10157660.html
Python 'xxx' codec can't decode byte xxx常见编码错相关推荐
- Pandas读取中文文本文件报错:python ‘utf-8‘ codec can‘t decode byte 0xe3 in position 0: unexpected end of data
近日用pandas的read_csv读取中文文本文件时报错:python 'utf-8' codec can't decode byte 0xe3 in position 0: unexpected ...
- Python 'gbk' codec can't decode byte 0x80
Python 'gbk' codec can't decode byte 0x80 前段时间,运行得好好的 Python 代码突然抛出如下错误: Traceback (most recent call ...
- python 'utf-8' codec can't decode byte 0xca in position 21: invalid continuation byte
今早起来肝网课的时候在通过路径把数据集导入到jupyter 里面的时候报了如下错误 'utf-8' codec can't decode byte 0xca in position 21: inval ...
- 解决Python:'utf-8' codec can't decode byte 0xfc in position 14: invalid start byte
读取了一个.amc的骨骼信息文件,然后报错: 'utf-8' codec can't decode byte 0xfc in position 14: invalid start byte 解决方法很 ...
- 解决UnicodeDecodeError: 'utf8' codec can't decode byte 0x9c
问题 这是在参照Yoon Kim的github实现论文Convolutional Neural Networks for Sentence Classification时遇到的问题.代码中使用的是经过 ...
- ‘gbk‘ codec can‘t decode byte 0x80 in position
大于127GBK无法解码 大于80超出了GBK编码范围 Python 'gbk' codec can't decode byte 0x80 删掉.decode("GBK").enc ...
- Python 技术篇 - 修改源码解决中文主机名导致的flask、socket服务起不来问题: ‘utf-8‘ codec can‘t decode byte 0xc0 in position...
由于主机名为中文导致的 flask 服务起不来,报错如下: File "D:\work\python3.9_64\lib\socket.py", line 791, in getf ...
- Python 的编码问题UnicodeDecodeError: 'ascii' codec can't decode byte ××× in postition
[问题描述] 运行环境, Ubuntu14.04 64bit 在用Python跑程序分析文本的时候出现了下面的报错信息: UnicodeDecodeError: 'ascii' codec can't ...
- 解决Python报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 658: illegal multibyte
解决Python报错–UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 658: illegal multibyte ...
最新文章
- 「云端 JavaScript 漫游指南」
- python2和python3如何共存_Windows下python2和python3的共存设置
- ffmpeg分析系列
- 牛客16502 螺旋矩阵
- 开发人员指南 Googlemap API教程
- PDF文件能编辑吗,怎么删除PDF文档中的空白页
- (转)Sublime Text 2 设置文件详解
- showModalDialog()、showModelessDialog()方法使用详解
- 宁德时代钠电池雷声大,雨点小?
- 金秋发布会·实在里程碑,从RPA 向 IPA 进军!
- ubuntu壁纸1080p
- 程序员自爆奇葩入职原因:进腾讯是为了不用续费年费会员,进抖音是想天天上班刷抖音
- 【Element ui 的NavMenu二级菜单下拉icon修改】
- adlds文件服务器,Windows轻型目录(AD LDS)的备份恢复
- 百万人同时在线直播的服务器,QQ游戏百万人同时在线的服务器架构实现
- 2020汽车消费把脉:如何挠到年轻消费者的内心?
- Facebook Rebound 弹性动画库 源码分析
- 安装Adobe Illustrator(AI) CC 2017 后弹出quot;无法启动此程序,因为计算机中丢失api-ms-win-crt-runtime-l1-1-0.dllquot;
- 制作哔哩哔哩弹幕词云
- MapReduce的分片机制源码解析
热门文章
- 在Hadoop中用Python实现MapReduce代码
- 云从科技助力智慧出行:登机安检更加“智能” | 百万人学AI评选
- BIM建筑环境规则和分析(BERA)语言介绍(四)第三章 BERA语言设计
- 【纯记录】谨以此文悼念我为此失去的时间|vim+pyclewn|
- 10年固守的小圈子,可能是未来最大的障碍
- DELL EqualLogic PS6100存储数据恢复方法和结构详解
- 生成函数(常见幂级数、广义二项式定理、生成函数的应用)
- 10A10-ASEMI整流二极管10A10
- ios安卓模拟器_【MOD 2.9】崩坏3 跨端/改服 安装包 V2.9.0 [iOS/安卓]
- CAD-强电常用符号集