【转】【Python】 python中的编码问题报错 'ascii' codec can't decode 及 URL地址获取中文
1.unicode、gbk、gb2312、utf-8的关系
http://www.pythonclub.org/python-basic/encode-detail 这篇文章写的比较好,utf-8是unicode的一种实现方式,unicode、gbk、gb2312是编码字符集;
2.python中的中文编码问题
2.1 .py文件中的编码
Python 默认脚本文件都是 ANSCII 编码的,当文件 中有非 ANSCII 编码范围内的字符的时候就要使用"编码指示"来修正。 一个module的定义中,如果.py文件中包含中文字符(严格的说是含有非anscii字符),则需要在第一行或第二行指定编码声明:
# -*- coding=utf-8 -*-或者 #coding=utf-8 其他的编码如:gbk、gb2312也可以; 否则会出现类似:SyntaxError: Non-ASCII character '/xe4' in file ChineseTest.py on line 1, but no encoding declared; see http://www.pytho for details这样的异常信息;n.org/peps/pep-0263.html
2.2 python中的编码与解码
先说一下python中的字符串类型,在python中有两种字符串类型,分别是str和unicode,他们都是basestring的派生类;str类型是一个包含Characters represent (at least) 8-bit bytes的序列;unicode的每个unit是一个unicode obj;所以:
len(u'中国')的值是2;len('ab')的值也是2;
在str的文档中有这样的一句话:The string data type is also used to represent arrays of bytes, e.g., to hold data read from a file. 也就是说在读取一个文件的内容,或者从网络上读取到内容时,保持的对象为str类型;如果想把一个str转换成特定编码类型,需要把str转为Unicode,然后从unicode转为特定的编码类型如:utf-8、gb2312等;
python中提供的转换函数:
unicode转为 gb2312,utf-8等
# -*- coding=UTF-8 -*-
if __name__ == '__main__':
s = u'中国'
s_gb = s.encode('gb2312')
utf-8,GBK转换为unicode 使用函数unicode(s,encoding) 或者s.decode(encoding)
# -*- coding=UTF-8 -*-
if __name__ == '__main__':
s = u'中国'
#s为unicode先转为utf-8
s_utf8 = s.encode('UTF-8')
assert(s_utf8.decode('utf-8') == s)
普通的str转为unicode
# -*- coding=UTF-8 -*-
if __name__ == '__main__':
s = '中国'
su = u'中国''
#s为unicode先转为utf-8
#因为s为所在的.py(# -*- coding=UTF-8 -*-)编码为utf-8
s_unicode = s.decode('UTF-8')
assert(s_unicode == su)
#s转为gb2312,先转为unicode再转为gb2312
s.decode('utf-8').encode('gb2312')
#如果直接执行s.encode('gb2312')会发生什么?
s.encode('gb2312')
# -*- coding=UTF-8 -*-
if __name__ == '__main__':
s = '中国'
#如果直接执行s.encode('gb2312')会发生什么?
s.encode('gb2312')
这里会发生一个异常:
Python 会自动的先将 s 解码为 unicode ,然后再编码成 gb2312。因为解码是python自动进行的,我们没有指明解码方式,python 就会使用 sys.defaultencoding 指明的方式来解码。很多情况下 sys.defaultencoding 是 ANSCII,如果 s 不是这个类型就会出错。
拿上面的情况来说,我的 sys.defaultencoding 是 anscii,而 s 的编码方式和文件的编码方式一致,是 utf8 的,所以出错了: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)
对于这种情况,我们有两种方法来改正错误:
一是明确的指示出 s 的编码方式
#! /usr/bin/env python
# -*- coding: utf-8 -*-
s = '中文'
s.decode('utf-8').encode('gb2312')
二是更改 sys.defaultencoding 为文件的编码方式
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys
reload(sys) # Python2.5 初始化后会删除 sys.setdefaultencoding 这个方法,我们需要重新载入
sys.setdefaultencoding('utf-8')
str = '中文'
str.encode('gb2312')
文件编码与print函数
建立一个文件test.txt,文件格式用ANSI,内容为:
abc中文
用python来读取
# coding=gbk
print open("Test.txt").read()
结果:abc中文
把文件格式改成UTF-8:
结果:abc涓枃
显然,这里需要解码:
# coding=gbk
import codecs
print open("Test.txt").read().decode("utf-8")
结果:abc中文
上面的test.txt我是用Editplus来编辑的,但当我用Windows自带的记事本编辑并存成UTF-8格式时,
运行时报错:
Traceback (most recent call last):
File "ChineseTest.py", line 3, in <module>
print open("Test.txt").read().decode("utf-8")
UnicodeEncodeError: 'gbk' codec can't encode character u'/ufeff' in position 0: illegal multibyte sequence
原来,某些软件,如notepad,在保存一个以UTF-8编码的文件时,会在文件开始的地方插入三个不可见的字符(0xEF 0xBB 0xBF,即BOM)。
因此我们在读取时需要自己去掉这些字符,python中的codecs module定义了这个常量:
# coding=gbk
import codecs
data = open("Test.txt").read()
if data[:3] == codecs.BOM_UTF8:
data = data[3:]
print data.decode("utf-8")
结果:abc中文
(四)一点遗留问题
在第二部分中,我们用unicode函数和decode方法把str转换成unicode。为什么这两个函数的参数用"gbk"呢?
第一反应是我们的编码声明里用了gbk(# coding=gbk),但真是这样?
修改一下源文件:
# coding=utf-8
s = "中文"
print unicode(s, "utf-8")
运行,报错:
Traceback (most recent call last):
File "ChineseTest.py", line 3, in <module>
s = unicode(s, "utf-8")
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 0-1: invalid data
显然,如果前面正常是因为两边都使用了gbk,那么这里我保持了两边utf-8一致,也应该正常,不至于报错。
更进一步的例子,如果我们这里转换仍然用gbk:
# coding=utf-8
s = "中文"
print unicode(s, "gbk")
结果:中文
python中的print原理:
When Python executes a print statement, it simply passes the output to the operating system (using fwrite() or something like it), and some other program is responsible for actually displaying that output on the screen. For example, on Windows, it might be the Windows console subsystem that displays the result. Or if you're using Windows and running Python on a Unix box somewhere else, your Windows SSH client is actually responsible for displaying the data. If you are running Python in an xterm on Unix, then xterm and your X server handle the display.
To print data reliably, you must know the encoding that this display program expects.
最后测试:
# coding=utf-8
s = "中文"
print unicode(s, "cp936")
# 结果:中文
这也可以解释为何如下输出不一致:
>>> s="哈哈"
>>> s
'\xe5\x93\x88\xe5\x93\x88'
>>> print s #这里为啥就可以呢? 见上文对print的解释
哈哈
>>> import sys
>>> sys.getdefaultencoding()
'ascii'
>>> print s.encode('utf8') # s在encode之前系统默认按ascii模式把s解码为unicode,然后再encode为utf8
Traceback (most recent call last):
File "<stdin>", line 1, in ?
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128)
>>> print s.decode('utf-8').encode('utf8')
哈哈
>>>
编码问题测试
使用 chardet 可以很方便的实现字符串/文件的编码检测
例子如下:
>>>
import
urllib >>>
rawdata = urllib
.urlopen
(
'http://www.google.cn/'
)
.read
(
) >>>
import
chardet
>>>
chardet.detect
(
rawdata)
{
'confidence'
: 0.98999999999999999
, 'encoding'
: 'GB2312'
}
chardet 下载地址 http://chardet.feedparser.org/
特别提示:
在工作中,经常遇到,读取一个文件,或者是从网页获取一个问题,明明看着是gb2312的编码,可是当使用decode转时,总是出错,这个时候,可以使用decode('gb18030')这个字符集来解决,如果还是有问题,这个时候,一定要注意,decode还有一个参数,比如,若要将某个String对象s从gbk内码转换为UTF-8,可以如下操作
s.decode('gbk').encode('utf-8′)
可是,在实际开发中,我发现,这种办法经常会出现异常:
UnicodeDecodeError: ‘gbk' codec can't decode bytes in position 30664-30665: illegal multibyte sequence
这 是因为遇到了非法字符——尤其是在某些用C/C++编写的程序中,全角空格往往有多种不同的实现方式,比如/xa3/xa0,或者/xa4/x57,这些 字符,看起来都是全角空格,但它们并不是“合法”的全角空格(真正的全角空格是/xa1/xa1),因此在转码的过程中出现了异常。
这样的问题很让人头疼,因为只要字符串中出现了一个非法字符,整个字符串——有时候,就是整篇文章——就都无法转码。
解决办法:
s.decode('gbk', ‘ignore').encode('utf-8′)
因为decode的函数原型是decode([encoding], [errors='strict']),可以用第二个参数控制错误处理的策略,默认的参数就是strict,代表遇到非法字符时抛出异常;
如果设置为ignore,则会忽略非法字符;
如果设置为replace,则会用?取代非法字符;
如果设置为xmlcharrefreplace,则使用XML的字符引用。
python文档
decode( [encoding[, errors]])
Decodes the string using the codec registered for encoding. encoding defaults to the default string encoding. errors may be given to set a different error handling scheme. The default is 'strict', meaning that encoding errors raise UnicodeError. Other possible values are 'ignore', 'replace' and any other name registered via codecs.register_error, see section 4.8.1.
详细出处参考:http://www.jb51.net/article/16104.htm
参考文献
【1】http://blog.chinaunix.net/u2/68206/showart.php?id=668359
【2】http://www.pythonclub.org/python-basic/codec
【3】http://www.pythonclub.org/python-scripts/quanjiao-banjiao
【4】http://www.pythonclub.org/python-basic/chardet
JS从URL上获取中文字符,默认获取到是二进制码:
encodeURI 方法返回一个编码的 URI。
decodeURI,返回初始的字符串即正常显示中文字符。
encodeURI 方法不会对下列字符进行编码:":"、"/"、";" 和 "?"。请使用 encodeURIComponent 方法对这些字符进行编码。
使用方法:decodeURI(url),encodeURI (url)
【转】【Python】 python中的编码问题报错 'ascii' codec can't decode 及 URL地址获取中文的更多相关文章
- python 3以上版本使用pickle.load读取文件报UnicodeDecodeError: 'ascii' codec can't decode byte 0x8b in position 6
python 3以上版本使用pickle.load读取文件报UnicodeDecodeError: 'ascii' codec can't decode byte 0x8b in position 6 ...
- 自动化上传图片,路径driver.find_element_by_id("oper-img-change").send_keys("C:\\Users\\76776\\Pictures\\logo.jpg"),为正确姿势,单\报错 'unicodeescape' codec can't decode bytes in position XXX: trun
自动化上传图片,路径driver.find_element_by_id("oper-img-change").send_keys("C:\\Users\\76776\\P ...
- python脚本中selenium启动浏览器报错os.path.basename(self.path), self.start_error_message) selenium.common.excep
在python脚本中,使用selenium启动浏览器报错,原因是未安装浏览器驱动,报错内容如下: # -*- coding:utf-8 -*-from selenium import webdrive ...
- python打开文件失败,报错'gbk' codec can't decode byte 0xbf in position 2: illegal multibyte sequence
python3.7,python3.6都存在的问题: 读取的文件编码是utf-8 第1行是空行.#开头都可能会报这个错误: E:\count_packet>python string_count ...
- 【错误记录】windows python 路径中的一个转义错误:'rawunicodeescape' codec can't decode bytes in position 112-113: truncated \uXXXX
ur"D:\work\结构化\CSV\useful\内容.csv" 报错 编码错误原因,当路径中有\u这种字串时,即使是包含在r"" 中也会进行转义,然后转义出 ...
- 更改python的编码问题:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 56: ordinal not in range(128)
as3:/usr/local/lib/python2.7/site-packages# cat sitecustomize.py # encoding=utf8 import sys reload(s ...
- python里面出现中文的时候报错 'ascii' codec can't encode characters in position
编码问题,在头部添加 import sys reload(sys) sys.setdefaultencoding( "utf-8" ) http://www.xuebuyuan.c ...
- pip 安装pandas报UnicodeDecodeError: 'ascii' codec can't decode byte 0xd5错
当Python在window环境中通过pip安装pandas报标题这样的错,主要是因为python默认编码格式是:ascii 在https://www.python.org/dev/peps/pep- ...
- Windows下面安装easy_install报UnicodeDecodeError: 'ascii' codec can't decode byte解决方法
在运行python ez_setup.py install后, 发现是在下载并解压setuptools-2.1,并运行setup.py时出现如下错误: 提示D:\Python27\lib\mimety ...
随机推荐
- android studio : clang++.exe: error: invalid linker name in argument '-fuse-ld=bfd
公司jenkins上的C++编译器最近换成了clang,今天更新了代码发现本地的C/C++代码用NDK编译不过了,提示: “clang++.exe: error: invalid linker nam ...
- .NET MVC+ EF+LINQ 多表联查VIEW显示列表
1.VIEW 页面显示代码 <link href="~/Content/bootstrap.css" rel="stylesheet" /> < ...
- python(49):把文件压缩成zip格式的文件
有时需要用到压缩文件,网上搜集了一段代码: 分享一下: import os import zipfile def make_zip(localPath, pname): zipf = zipfile. ...
- 分析jvm线程堆栈
目录 一.java线程状态 二.使用jstack生成进程dump文件 三.统计dump文件中处于不同状态的线程数量 四.举例分析不同状态的线程 1.分析BLOCKED (on object monit ...
- LeetCode: Binary Search Tree Iterator 解题报告
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
- poj1753(位运算压缩状态+bfs)
题意:有个4*4的棋盘,上面摆着黑棋和白旗,b代表黑棋,w代表白棋,现在有一种操作,如果你想要改变某一个棋子的颜色,那么它周围(前后左右)棋子的颜色都会被改变(白变成黑,黑变成白),问你将所有棋子变成 ...
- Oracle数据库密码过期
按照如下步骤进行操作:1.查看用户的proifle是哪个,一般是default: SQL>SELECT USERNAME,PROFILE FROM DBA_USERS; 2.查看指定概要文件(如 ...
- 5. 支持向量机(SVM)软间隔
1. 感知机原理(Perceptron) 2. 感知机(Perceptron)基本形式和对偶形式实现 3. 支持向量机(SVM)拉格朗日对偶性(KKT) 4. 支持向量机(SVM)原理 5. 支持向量 ...
- css3 data-attribute属性打造漂亮的按钮
之前介绍了几款css3实现的按钮,今天为网友来款比较新鲜的,用css3的data-attribute属性开发按钮,当鼠标经过显示按钮的详细信息.而且实现过程很简单,几行代码就搞定.大家试一试吧.如下图 ...
- Don’t Put View Code Into Your View Controller别把View创建的代码放在VC中(swift)
Don't Put Into Your View Controller别把View创建的代码放在VC中html, body {overflow-x: initial !important;}.Code ...