Python - 编码转换

# coding: utf-8

s = 'abc'
print type(s) # str(utf-8)
print len(s) # 3

s = unicode(s) # str -> unicode,其中str的每个字符值必须小于128
print type(s) # unicode
print len(s) # 3

s = u'abc'
print type(s) # unicode
print len(s) # 3

s = s.encode('utf-8') # unicode -> str(utf-8)
print type(s) # str
print len(s) # 3

s = s.decode('utf-8') # str(utf-8) -> unicode,这里str的每个字符值任意
print type(s) # unicode
print len(s) # 3

s = '中国' # 由于整个文件以utf-8编码
print type(s) # str(utf-8)
print len(s) # 6

s = u'中国'
print type(s) # unicode
print len(s) # 2

s = s.encode('utf-8')
print type(s) # str(utf-8)
print len(s) # 6

s = s.decode('utf-8')
print type(s) # unicode
print len(s) # 2

s = raw_input(u'输入:') # windows下貌似中文按gbk编码,每个中文占2个字节
print type(s) # str(gbk)
print len(s) # 4

s = s.decode('gbk') # 要想gbk编码转为utf-8编码,先将gbk编码转为unicode
print type(s) # unicode
print len(s) # 2

s = s.encode('gbk')
print type(s) # str(gbk)
print len(s) # 4

# 根据以上的验证,得出结论
# 各种编码都可以通过unicode来转化,unicode可以假想为一张各种字符的对照表,在这个表中可以找到世界范围内的任何一种字符
# 当然,也包括中文,每个字符都对应一个序号,如'a' -> 0x61,'中' -> 0x4e2d
# unicode -> utf-8 unicode.encode('utf-8')
# utf-8 -> unicode str.decode('utf-8')
# gbk -> unicode str.decode('gbk')
# unicode -> gbk unicode.encode('gbk')

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值