Python 字符编码,python字符编码,#coding=utf-
文章由Byrx.net分享于2019-03-23 07:03:56
Python 字符编码,python字符编码,#coding=utf-
#coding=utf-8 import sys print sys.getdefaultencoding() # --> ascii u1 = '中国' print type(u1), repr(u1) # --> <type 'str'> '\\xe4\\xb8\\xad\\xe5\\x9b\\xbd' u2 = u'中国2009' print type(u2), repr(u2) # --> <type 'unicode'> u'\\u4e2d\\u56fd2009' # str --> unicode print print '# str --> unicode' u1_1 = u1.decode('utf8') print type(u1_1), repr(u1_1) # --> <type 'unicode'> u'\\u4e2d\\u56fd' u1_2 = unicode(u1, 'utf8') print type(u1_2), repr(u1_2) # --> <type 'unicode'> u'\\u4e2d\\u56fd' # unicode --> str print print '# unicode --> str' u2_1 = u2.encode('utf8') print type(u2_1), repr(u2_1) # --> <type 'str'> '\\xe4\\xb8\\xad\\xe5\\x9b\\xbd2009' u2_2 = u2.encode('gbk') print type(u2_2), repr(u2_2) # --> <type 'str'> '\\xd6\\xd0\\xb9\\xfa2009' u2_3 = u2.encode('gb2312') print type(u2_3), repr(u2_3) # --> <type 'str'> '\\xd6\\xd0\\xb9\\xfa2009' #该片段来自于http://byrx.net
评论关闭