编码与解码

python中文件编码默认为utf-8,字符编码默认为unicode,

一个例子:

s = '我家小馆'
print(s)
unicode_to_utf8 = s.encode('utf-8')
unicode_to_gbk = s.encode('gbk')
print('utf8:',unicode_to_utf8)
print('gbk:',unicode_to_gbk)

gbk_to_unicode = unicode_to_gbk.decode('gbk')
unicode_to_utf8 = gbk_to_unicode.encode('utf-8')

print(gbk_to_unicode)
print(unicode_to_utf8)

结果为:
我家小馆
utf8: b'\xe6\x88\x91\xe5\xae\xb6\xe5\xb0\x8f\xe9\xa6\x86'
gbk: b'\xce\xd2\xbc\xd2\xd0\xa1\xb9\xdd'
我家小馆
b'\xe6\x88\x91\xe5\xae\xb6\xe5\xb0\x8f\xe9\xa6\x86'
可以看出unicode编码的字符可以直接显示汉字,
而utf-8的汉字为三个字节一个字符,gbk的汉字为两个字符一个汉字。
从unicode-->utf8为编码encode
从utf8-->unicode为解码decode

转载于:https://www.cnblogs.com/lokerx/p/10708844.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值