欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

关于python中文编码乱码问题的两篇文章汇总

程序员文章站 2022-07-05 16:11:42
...
python中文编码乱码问题

背景

多次被python的编码/乱码问题困扰,相信pythoner们都被困扰过,网上铺天盖地的资料太多也参差不齐,就整理了下。本文从使用的角度系统总结了python编码相关的一些概念,将本文的例子玩一遍,基本上对python的编码问题就清楚了。

首先明确几个概念:

  1. 字节流:以utf8/gbk等编码编码的字节流。
  2. unicode对象:python代码中,a=u'中国', 或者a='中国'.decode()的结果。
  3. terminal用于显示字符的编码:将一个用utf8/gbk编码的字节流通过terminal指定的编码,去查找对应的字符显示出来。
  4. locale:linux下,Locale 是软件在运行时的语言环境, 它包括语言(Language), 地域 (Territory) 和字符集(Codeset)。一个locale的书写格式为: 语言[_地域[.字符集]]. 所以说呢,locale总是和一定的字符集相联系的。比如:zh_CN.GB2312
  5. 编码转换原则:unicode是”中介”,任何编码之间转换都需要先decode()到unicode。

针对python,先把结论放在前面,三点:

  1. #coding:utf-8 #.py文件是什么编码就需要告诉python用什么编码去读取这个.py文件。
  2. sys.stdout.encoding,默认就是locale的编码,print会用sys.stdout.encoding去encode()成字节流,交给terminal显示。所以locale需要与terminal一致,才能正确print打印出中文。
  3. sys.setdefaultencoding(‘utf8’),用于指定str.encode() str.decode()的默认编码,默认是ascii。
    • 对编码字符串a,代码中可以直接写a.encode(“gbk”),但事实上内部自动先通过defaultencoding 去decode成unicode之后再encode()的。
    • str(xxx)应该也是用这个去编码的。
    • 'ascii' codec can't encode characters in position 7-8: ordinal not in range(128)print的时候出现这个错误一般可以使用这个方案去处理。
    • 为了避免代码中到处都要去encode(“xxx”),还有可能不同的地方写得不一样带来不一致的情况,推荐使用这个:
import sys   
reload(sys)   
sys.setdefaultencoding('utf8')   
  • 1
  • 2
  • 3

例子1:

  • 在python中,unicode vs 字节流:字节流可以从unicode encode得到,unicode可以从utf8/gbk等编码的字节流decode得到。
  • 分析下面这段代码,终端/locale分别为不同编码的情况:
#coding:utf-8              #由于.py文件是utf-8的,所以必须有这一句
import sys
import locale
import os
import codecs

reload(sys)
print sys.getdefaultencoding() + "  - sys.getdefaultencoding()"
sys.setdefaultencoding('utf8')                  #影响encode()
print sys.getdefaultencoding() + "  - sys.getdefaultencoding()"

print sys.stdout.encoding + " - sys.stdout.encoding:"
#sys.stdout = codecs.getwriter('utf8')(sys.stdout)    #影响print
print sys.stdout.encoding + " - sys.stdout.encoding:"

u = u'中国'
print u + "  - u"
a = '中国'
print a + " - a"
print a.decode('utf-8') + " - a.decode('utf-8')"
print a.decode('utf-8').encode('gbk') + "   - a.decode('utf-8').encode('gbk')"
print a.decode('utf-8').encode('utf-8') + " - a.decode('utf-8').encode('utf-8')"
print a.decode('utf-8').encode() + "    - a.decode('utf-8').encode()"

print (sys.stdout.encoding) + " - (sys.stdout.encoding)"
print (sys.stdout.isatty())
print (locale.getpreferredencoding())
print (sys.getfilesystemencoding())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

—终端为UTF-8,locale为zh_CN.GBK—————–

ascii  - sys.getdefaultencoding()
utf8  - sys.getdefaultencoding()
GBK - sys.stdout.encoding:
GBK - sys.stdout.encoding:
�й�  - u
中国 - a
�й� - a.decode('utf-8')
�й�   - a.decode('utf-8').encode('gbk')
中国 - a.decode('utf-8').encode('utf-8')
中国    - a.decode('utf-8').encode()
GBK - (sys.stdout.encoding)
True
GBK
utf-8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

—终端为UTF-8,locale为zh_CN.UTF-8—————–

ascii  - sys.getdefaultencoding()
utf8  - sys.getdefaultencoding()
UTF-8 - sys.stdout.encoding:
UTF-8 - sys.stdout.encoding:
中国  - u
中国 - a
中国 - a.decode('utf-8')
�й�   - a.decode('utf-8').encode('gbk')
中国 - a.decode('utf-8').encode('utf-8')
中国    - a.decode('utf-8').encode()
UTF-8 - (sys.stdout.encoding)
True
UTF-8
utf-8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

—终端为GBK,locale为zh_CN.GBK—————–

ascii  - sys.getdefaultencoding()
utf8  - sys.getdefaultencoding()
GBK - sys.stdout.encoding:
GBK - sys.stdout.encoding:
中国  - u
涓???? - a
中国 - a.decode('utf-8')
中国   - a.decode('utf-8').encode('gbk')
涓???? - a.decode('utf-8').encode('utf-8')
涓????    - a.decode('utf-8').encode()
GBK - (sys.stdout.encoding)
True
GBK
utf-8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

—终端为GBK,locale为zh_CN.UTF-8—————–

ascii  - sys.getdefaultencoding()
utf8  - sys.getdefaultencoding()
UTF-8 - sys.stdout.encoding:
UTF-8 - sys.stdout.encoding:
涓????  - u
涓???? - a
涓???? - a.decode('utf-8')
中国   - a.decode('utf-8').encode('gbk')
涓???? - a.decode('utf-8').encode('utf-8')
涓????    - a.decode('utf-8').encode()
UTF-8 - (sys.stdout.encoding)
True
UTF-8
utf-8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

例子1总结,对print而言:

  • unicode的数据如果要显示正常,必须终端与locale一致。sys.stdout.encoding这个值应该来自locale,print会以sys.stdout.encoding去encode并输出到字节流。
  • encode为终端编码的字节流就能显示正常,无论locale是啥。
    最终是terminal通过terminal配置的编码规则去解码成对应的字符并显示出来。

例子2:

关于sys.setdefaultencoding(‘utf8’)的例子:

#coding:utf-8
import sys

reload(sys)
sys.setdefaultencoding('utf8')
print sys.getdefaultencoding() + "  - sys.getdefaultencoding()"

a = '中国'
print a + " - a"
print a.encode("gbk")  #并不是直接从utf8的字节流转化到gbk的,而是通过defaultencoding decode之后才转的。
print a.decode() #使用默认的defaultencoding
print a.encode() #使用默认的defaultencoding
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

关于str()和repr()

  • str()是对各种类型转化成str,如果本来是encoded字符串,则不变,如果为unicode,会encode()
  • repr()对字符串是将字节流出二进制的值以16进制转化为可见字符。
    测试环境locale为GBK
#coding:utf-8

import sys
reload(sys)
sys.setdefaultencoding("utf-8")

a = u'中国'
print a
print str(a)
print repr(a)
print repr(a.encode("utf-8"))
print repr(a.encode("gbk"))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
中国
涓????
u'\u4e2d\u56fd'
'\xe4\xb8\xad\xe5\x9b\xbd'
'\xd6\xd0\xb9\xfa'
  • 1
  • 2
  • 3
  • 4
  • 5

再深挖下去,还有repr()和eval()的关系,就不深挖了。

关于终端和服务器的编码

另外补充一些关于终端和服务器编码的结论:
1. 对mac iterm2,如果server的locale与mac本地终端的locale一致,才能保证server端与本地的表现一致。
2. cat a.py #就把文件显示出来,就是给terminal一串字节流。terminal根据设置的终端编码规则来显示字符。所以只要文件编码与terminal一致即可,与locale无关。
3. cat a.txt > b.txt #无论locale怎么样,只跟a.txt原来的编码相关
4. echo “中国年过” > a.txt #这个情况下,只有terminal与locale的编码一致,你才能在终端shell打出正确的中文~~~所以a.txt与两者都会一致

参考资料

关于vim:http://blog.chinaunix.net/uid-21843387-id-106001.html






老是遇到编码问题,快折腾死了,找到一篇不错的,转载一下

这个问题在python3.0里已经解决了。


这有篇很好的文章,可以明白这个问题:

为什么会报错“UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 0-1: ordinal not in range(128)”?本文就来研究一下这个问题。
字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unicode编码(encode)成另一种编码。

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的编码形式


>>>>>
groups.google.com/group/python-cn/browse_thread/thread/be4e4e0d4c3272dd
—–
python是个容易出现编码问题的语言。所以,我按照我的理解写下下面这些文字。
=首先,要了解几个概念。=
*字节:计算机数据的表示。8位二进制。可以表示无符号整数:0-255。下文,用“字节流”表示“字节”组成的串。
*字符:英文字符“abc”,或者中文字符“你我他”。字符本身不知道如何在计算机中保存。下文中,会避免使用“字符串”这个词,而用“文本”来表
示“字符”组成的串。
*编码(动词):按照某种规则(这个规则称为:编码(名词))将“文本”转换为“字节流”。(在python中:unicode变成str)
*解码(动词):将“字节流”按照某种规则转换成“文本”。(在python中:str变成unicode)
**实际上,任何东西在计算机中表示,都需要编码。例如,视频要编码然后保存在文件中,播放的时候需要解码才能观看。
unicode:unicode定义了,一个“字符”和一个“数字”的对应,但是并没有规定这个“数字”在计算机中怎么保存。(就像在C中,一个整数既
可以是int,也可以是short。unicode没有规定用int还是用short来表示一个“字符”)
utf8:unicode实现。它使用unicode定义的“字符”“数字”映射,进而规定了,如何在计算机中保存这个数字。其它的utf16等都是
unicode实现。
gbk:类似utf8这样的“编码”。但是它没有使用unicode定义的“字符”“数字”映射,而是使用了另一套的映射方法。而且,它还定义了如何在
计算机中保存。

=python中的encode,decode方法=
首先,要知道encode是 unicode转换成str。decode是str转换成unicode。
下文中,u代表unicode类型的变量,s代表str类型的变量。
u.encode(‘…’)基本上总是能成功的,只要你填写了正确的编码。就像任何文件都可以压缩成zip文件。
s.decode(‘…’)经常是会出错的,因为str是什么“编码”取决于上下文,当你解码的时候需要确保s是用什么编码的。就像,打开zip文
件的时候,你要确保它确实是zip文件,而不仅仅是伪造了扩展名的zip文件。
u.decode(),s.encode()不建议使用,s.encode相当于s.decode().encode()首先用默认编码(一般是
ascii)转换成unicode在进行encode。

=关于#coding=utf8=
当你在py文件的第一行中,写了这句话,并确实按照这个编码保存了文本的话,那么这句话有以下几个功能。
1.使得词法分析器能正常运作,对于注释中的中文不报错了。
2.对于u”中文”这样literal string能知道两个引号中的内容是utf8编码的,然后能正确转换成unicode
3.”中文”对于这样的literal string你会知道,这中间的内容是utf8编码,然后就可以正确转换成其它编码或unicode了。

没有写完,先码那么多字,以后再来补充,这里不是wiki,太麻烦了。

>>>>>
>>>>>
=Python编码和Windows控制台=
我发现,很多初学者出错的地方都在print语句,这牵涉到控制台的输出。我不了解linux,所以只说控制台的。
首先,Windows的控制台确实是unicode(utf16_le编码)的,或者更准确的说使用字符为单位输出文本的。
但是,程序的执行是可以被重定向到文件的,而文件的单位是“字节”。
所以,对于C运行时的函数printf之类的,输出必须有一个编码,把文本转换成字节。可能是为了兼容95,98,
没有使用unicode的编码,而是mbcs(不是gbk之类的)。
windows的mbcs,也就是ansi,它会在不同语言的windows中使用不同的编码,在中文的windows中就是gb系列的编码。
这造成了同一个文本,在不同语言的windows中是不兼容的。
现在我们知道了,如果你要在windows的控制台中输出文本,它的编码一定要是“mbcs”。
对于python的unicode变量,使用print输出的话,会使用sys.getfilesystemencoding()返回的编码,把它变成str。
如果是一个utf8编码str变量,那么就需要 print s.decode(‘utf8’).encode(‘mbcs’)
最后,对于str变量,file文件读取的内容,urllib得到的网络上的内容,都是以“字节”形式的。
它们如果确实是一段“文本”,比如你想print出来看看。那么你必须知道它们的编码。然后decode成unicode。
如何知道它们的编码:
1.事先约定。(比如这个文本文件就是你自己用utf8编码保存的)
2.协议。(python文件第一行的#coding=utf8,html中的<meta>等)
3.猜。

>>>>>


背景

多次被python的编码/乱码问题困扰,相信pythoner们都被困扰过,网上铺天盖地的资料太多也参差不齐,就整理了下。本文从使用的角度系统总结了python编码相关的一些概念,将本文的例子玩一遍,基本上对python的编码问题就清楚了。

首先明确几个概念:

  1. 字节流:以utf8/gbk等编码编码的字节流。
  2. unicode对象:python代码中,a=u'中国', 或者a='中国'.decode()的结果。
  3. terminal用于显示字符的编码:将一个用utf8/gbk编码的字节流通过terminal指定的编码,去查找对应的字符显示出来。
  4. locale:linux下,Locale 是软件在运行时的语言环境, 它包括语言(Language), 地域 (Territory) 和字符集(Codeset)。一个locale的书写格式为: 语言[_地域[.字符集]]. 所以说呢,locale总是和一定的字符集相联系的。比如:zh_CN.GB2312
  5. 编码转换原则:unicode是”中介”,任何编码之间转换都需要先decode()到unicode。

针对python,先把结论放在前面,三点:

  1. #coding:utf-8 #.py文件是什么编码就需要告诉python用什么编码去读取这个.py文件。
  2. sys.stdout.encoding,默认就是locale的编码,print会用sys.stdout.encoding去encode()成字节流,交给terminal显示。所以locale需要与terminal一致,才能正确print打印出中文。
  3. sys.setdefaultencoding(‘utf8’),用于指定str.encode() str.decode()的默认编码,默认是ascii。
    • 对编码字符串a,代码中可以直接写a.encode(“gbk”),但事实上内部自动先通过defaultencoding 去decode成unicode之后再encode()的。
    • str(xxx)应该也是用这个去编码的。
    • 'ascii' codec can't encode characters in position 7-8: ordinal not in range(128)print的时候出现这个错误一般可以使用这个方案去处理。
    • 为了避免代码中到处都要去encode(“xxx”),还有可能不同的地方写得不一样带来不一致的情况,推荐使用这个:
import sys   
reload(sys)   
sys.setdefaultencoding('utf8')   
  • 1
  • 2
  • 3

例子1:

  • 在python中,unicode vs 字节流:字节流可以从unicode encode得到,unicode可以从utf8/gbk等编码的字节流decode得到。
  • 分析下面这段代码,终端/locale分别为不同编码的情况:
#coding:utf-8              #由于.py文件是utf-8的,所以必须有这一句
import sys
import locale
import os
import codecs

reload(sys)
print sys.getdefaultencoding() + "  - sys.getdefaultencoding()"
sys.setdefaultencoding('utf8')                  #影响encode()
print sys.getdefaultencoding() + "  - sys.getdefaultencoding()"

print sys.stdout.encoding + " - sys.stdout.encoding:"
#sys.stdout = codecs.getwriter('utf8')(sys.stdout)    #影响print
print sys.stdout.encoding + " - sys.stdout.encoding:"

u = u'中国'
print u + "  - u"
a = '中国'
print a + " - a"
print a.decode('utf-8') + " - a.decode('utf-8')"
print a.decode('utf-8').encode('gbk') + "   - a.decode('utf-8').encode('gbk')"
print a.decode('utf-8').encode('utf-8') + " - a.decode('utf-8').encode('utf-8')"
print a.decode('utf-8').encode() + "    - a.decode('utf-8').encode()"

print (sys.stdout.encoding) + " - (sys.stdout.encoding)"
print (sys.stdout.isatty())
print (locale.getpreferredencoding())
print (sys.getfilesystemencoding())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

—终端为UTF-8,locale为zh_CN.GBK—————–

ascii  - sys.getdefaultencoding()
utf8  - sys.getdefaultencoding()
GBK - sys.stdout.encoding:
GBK - sys.stdout.encoding:
�й�  - u
中国 - a
�й� - a.decode('utf-8')
�й�   - a.decode('utf-8').encode('gbk')
中国 - a.decode('utf-8').encode('utf-8')
中国    - a.decode('utf-8').encode()
GBK - (sys.stdout.encoding)
True
GBK
utf-8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

—终端为UTF-8,locale为zh_CN.UTF-8—————–

ascii  - sys.getdefaultencoding()
utf8  - sys.getdefaultencoding()
UTF-8 - sys.stdout.encoding:
UTF-8 - sys.stdout.encoding:
中国  - u
中国 - a
中国 - a.decode('utf-8')
�й�   - a.decode('utf-8').encode('gbk')
中国 - a.decode('utf-8').encode('utf-8')
中国    - a.decode('utf-8').encode()
UTF-8 - (sys.stdout.encoding)
True
UTF-8
utf-8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

—终端为GBK,locale为zh_CN.GBK—————–

ascii  - sys.getdefaultencoding()
utf8  - sys.getdefaultencoding()
GBK - sys.stdout.encoding:
GBK - sys.stdout.encoding:
中国  - u
涓???? - a
中国 - a.decode('utf-8')
中国   - a.decode('utf-8').encode('gbk')
涓???? - a.decode('utf-8').encode('utf-8')
涓????    - a.decode('utf-8').encode()
GBK - (sys.stdout.encoding)
True
GBK
utf-8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

—终端为GBK,locale为zh_CN.UTF-8—————–

ascii  - sys.getdefaultencoding()
utf8  - sys.getdefaultencoding()
UTF-8 - sys.stdout.encoding:
UTF-8 - sys.stdout.encoding:
涓????  - u
涓???? - a
涓???? - a.decode('utf-8')
中国   - a.decode('utf-8').encode('gbk')
涓???? - a.decode('utf-8').encode('utf-8')
涓????    - a.decode('utf-8').encode()
UTF-8 - (sys.stdout.encoding)
True
UTF-8
utf-8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

例子1总结,对print而言:

  • unicode的数据如果要显示正常,必须终端与locale一致。sys.stdout.encoding这个值应该来自locale,print会以sys.stdout.encoding去encode并输出到字节流。
  • encode为终端编码的字节流就能显示正常,无论locale是啥。
    最终是terminal通过terminal配置的编码规则去解码成对应的字符并显示出来。

例子2:

关于sys.setdefaultencoding(‘utf8’)的例子:

#coding:utf-8
import sys

reload(sys)
sys.setdefaultencoding('utf8')
print sys.getdefaultencoding() + "  - sys.getdefaultencoding()"

a = '中国'
print a + " - a"
print a.encode("gbk")  #并不是直接从utf8的字节流转化到gbk的,而是通过defaultencoding decode之后才转的。
print a.decode() #使用默认的defaultencoding
print a.encode() #使用默认的defaultencoding
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

关于str()和repr()

  • str()是对各种类型转化成str,如果本来是encoded字符串,则不变,如果为unicode,会encode()
  • repr()对字符串是将字节流出二进制的值以16进制转化为可见字符。
    测试环境locale为GBK
#coding:utf-8

import sys
reload(sys)
sys.setdefaultencoding("utf-8")

a = u'中国'
print a
print str(a)
print repr(a)
print repr(a.encode("utf-8"))
print repr(a.encode("gbk"))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
中国
涓????
u'\u4e2d\u56fd'
'\xe4\xb8\xad\xe5\x9b\xbd'
'\xd6\xd0\xb9\xfa'
  • 1
  • 2
  • 3
  • 4
  • 5

再深挖下去,还有repr()和eval()的关系,就不深挖了。

关于终端和服务器的编码

另外补充一些关于终端和服务器编码的结论:
1. 对mac iterm2,如果server的locale与mac本地终端的locale一致,才能保证server端与本地的表现一致。
2. cat a.py #就把文件显示出来,就是给terminal一串字节流。terminal根据设置的终端编码规则来显示字符。所以只要文件编码与terminal一致即可,与locale无关。
3. cat a.txt > b.txt #无论locale怎么样,只跟a.txt原来的编码相关
4. echo “中国年过” > a.txt #这个情况下,只有terminal与locale的编码一致,你才能在终端shell打出正确的中文~~~所以a.txt与两者都会一致

参考资料

关于vim:http://blog.chinaunix.net/uid-21843387-id-106001.html






老是遇到编码问题,快折腾死了,找到一篇不错的,转载一下

这个问题在python3.0里已经解决了。


这有篇很好的文章,可以明白这个问题:

为什么会报错“UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 0-1: ordinal not in range(128)”?本文就来研究一下这个问题。
字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unicode编码(encode)成另一种编码。

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



相关标签: 编码 解码