一.python2
python2中默认以ASCII编码
str='hello world'
gbk_to_unicode=str.decode('gbk')#将gbk解码为unicode
print(gbk_to_unicode)
unicode_to_utf-8=gbk_to_unicode.encode('utf-8')#将unicode编码为utf-8
print(unicode_to_utf-8)
二.python3
python3文本默认是以Unicode编码
str='hello world'
unicode_to_gbk=str.encode('gbk')
print(unicode_to_gbk)#此处输出为byte类型的gbk编码的内容,即在编码的同时将字符串转换成了byte类型
gbk_to_utf-8=unicode_to_gbk.decode('utf-8')#此处在解码的同时将byte转换成了字符串
print(gbk_to_utf-8)
感觉此处内容有些复杂,不敢确认上述内容是否正确,还请路过的前辈补充指点。
继续阅读与本文标签相同的文章
-
免费下载!《阿里工程师的自我修养》公开10位阿里大牛解决问题的思维方式
2026-05-18栏目: 教程
-
BAT技术面dubbo还能这么问?
2026-05-18栏目: 教程
-
yaml转json,json转yaml,使用js-yaml实现yaml和json转换
2026-05-18栏目: 教程
-
PostgreSQL系统隐藏字段
2026-05-18栏目: 教程
-
7月24日阿里云峰会.上海 开发者大会回看
2026-05-18栏目: 教程
