encode和decode
Python字符串的encode与decode研究心得乱码问题解决方法
为什么会报错“UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)”?本文就来研究一下这个问题。
decode的作用是将其他编码的字符串转换成unicode编码,如str1.decode('gb2312'),表示将gb2312编码的字符串str1转换成unicode编码。
encode的作用是将unicode编码转换成其他编码的字符串,如str2.encode('gb2312'),表示将unicode编码的字符串str2转换成gb2312编码。
因此,转码的时候一定要先搞明白,字符串str是什么编码,然后decode成unicode,然后再encode成其他编码
代码中字符串的默认编码与代码文件本身的编码一致。
如:s='中文'
如果是在utf8的文件中,该字符串就是utf8编码,如果是在gb2312的文件中,则其编码为gb2312。这种情况下,要进行编码转换,都需要先用decode方法将其转换成unicode编码,再使用encode方法将其转换成其他编码。通常,在没有指定特定的编码方式时,都是使用的系统默认编码创建的代码文件。
如果字符串是这样定义:s=u'中文'
则该字符串的编码就被指定为unicode了,即python的内部编码,而与代码文件本身的编码无关。因此,对于这种情况做编码转换,只需要直接使用encode方法将其转换成指定编码即可。
如果一个字符串已经是unicode了,再进行解码则将出错,因此通常要对其编码方式是否为unicode进行判断:
isinstance(s, unicode) #用来判断是否为unicode
用非unicode编码形式的str来encode会报错
如何获得系统的默认编码?
#!/usr/bin/env python
#coding=utf-8
import sys
print sys.getdefaultencoding()
该段程序在英文WindowsXP上输出为:ascii
在某些IDE中,字符串的输出总是出现乱码,甚至错误,其实是由于IDE的结果输出控制台自身不能显示字符串的编码,而不是程序本身的问题。
如在UliPad中运行如下代码:
s=u"中文"
print s
会提示:UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)。这是因为UliPad在英文WindowsXP上的控制台信息输出窗口是按照ascii编码输出的(英文系统的默认编码是ascii),而上面代码中的字符串是Unicode编码的,所以输出时产生了错误。
将最后一句改为:print s.encode('gb2312')
则能正确输出“中文”两个字。
若最后一句改为:print s.encode('utf8')
则输出:\xe4\xb8\xad\xe6\x96\x87,这是控制台信息输出窗口按照ascii编码输出utf8编码的字符串的结果。
unicode(str,'gb2312')与str.decode('gb2312')是一样的,都是将gb2312编码的str转为unicode编码
使用str.__class__可以查看str的编码形式
原理说了半天,最后来个包治百病的吧:)
#!/usr/bin/env python
#coding=utf-8
s="中文" if isinstance(s, unicode):
#s=u"中文"
print s.encode('gb2312')
else:
#s="中文"
print s.decode('utf-8').encode('gb2312')
encode和decode的更多相关文章
- [LeetCode] Encode and Decode Strings 加码解码字符串
		
Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...
 - 【python】python新手必碰到的问题---encode与decode,中文乱码[转]
		
转自:http://blog.csdn.net/a921800467b/article/details/8579510 为什么会报错“UnicodeEncodeError:'ascii' codec ...
 - LeetCode Encode and Decode Strings
		
原题链接在这里:https://leetcode.com/problems/encode-and-decode-strings/ 题目: Design an algorithm to encode a ...
 - Encode and Decode Strings
		
Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...
 - 【python】浅谈encode和decode
		
对于encode和decode,笔者也是根据自己的理解,有不对的地方还请多多指点. 编码的理解: 1.编码:utf-8,utf-16,gbk,gb2312,gb18030等,编码为了便于理解,可以把它 ...
 - 271. Encode and Decode Strings
		
题目: Design an algorithm to encode a list of strings to a string. The encoded string is then sent ove ...
 - [LeetCode#271] Encode and Decode Strings
		
Problem: Design an algorithm to encode a list of strings to a string. The encoded string is then sen ...
 - Encode and Decode Strings 解答
		
Question Design an algorithm to encode a list of strings to a string. The encoded string is then sen ...
 - python encode和decode函数说明【转载】
		
python encode和decode函数说明 字符串编码常用类型:utf-8,gb2312,cp936,gbk等. python中,我们使用decode()和encode()来进行解码和编码 在p ...
 
随机推荐
- Spring-编程式事物
			
所谓编程式事务指的是通过编码方式实现事务,即类似于JDBC编程实现事务管理. Spring框架提供一致的事务抽象,因此对于JDBC还是JTA事务都是采用相同的API进行编程. Connection c ...
 - 【CodeForces 504A】Misha and Forest
			
题 题意 有n个点,代号分别为0到n-1,然后这n个点有d个相连点,相连点的XOR sum 为s,求所有的边. 分析 知识:a^b^a=b,a^b^b=a. 把相连点为1的i存进队列,i的唯一相连点就 ...
 - 【HDU 1228】A + B
			
题 Description 读入两个小于100的正整数A和B,计算A+B. 需要注意的是:A和B的每一位数字由对应的英文单词给出. Input 测试输入包含若干测试用例,每个测试用例占一行,格式 ...
 - Android中实现自定义的拍照应用
			
可以参考:http://www.android-doc.com/guide/topics/media/camera.html 一.添加相应的权限 <uses-permission android ...
 - python 学习笔记3(循环方式;list初始化;循环对象/生成器/表推导;函数对象;异常处理)
			
### Python的强大很大一部分原因在于,它提供有很多已经写好的,可以现成用的对象 16. 循环方式笔记: 1)range(0, 8, 2) #(上限,下限,步长) 可以实现对元素或者下标的 ...
 - 【poj3177】 Redundant Paths
			
http://poj.org/problem?id=3177 (题目链接) 题意 给出一个n个节点m条边的无向图,求最少连几条边使图中没有桥. Solution 我们可以发现,用最少的边使得图中没有桥 ...
 - 【bzoj2463】 谁能赢呢?
			
www.lydsy.com/JudgeOnline/problem.php?id=2463 (题目链接) 题意 一个n*n的棋盘,开始时左上角有一个棋子,每次可以把棋子向4个方向移动,但不能移动到曾经 ...
 - android studio问题-ICCP:Not recognizing known sRGB profile
			
转:http://my.oschina.net/1pei/blog/479162 PNG格式:每个PNG文件是由一个PNG标识(signature),后面跟一些数据块(chunk),每个chunk由 ...
 - Latex 笔记
			
A source file is made up of text, formulas, and instructions (commands) to $\LaTeX.$ Commands start ...
 - ZOJ 3430 Detect the Virus
			
传送门: Detect the Virus ...