首页 > Web开发 > 详细

编码 decode & encode

时间:2018-04-28 11:17:24      阅读:219      评论:0      收藏:0      [点我收藏+]

技术分享图片

 

 

import sys
 
# python3 中字符编码默认为 utf-8
s = ‘你好‘
print(s)

# utf-8 转为 gbk (s 默认为 unicode 所以可以直接 encode 成 gbk)
s_to_gbk = s.encode(‘gbk‘)
print(‘gbk‘, s_to_gbk)
print(‘default‘, s.encode())
print(‘bytes type‘, type(s))
print(‘system default‘, sys.getdefaultencoding())

# 将 gbk 再转为 utf-8 , 因为默认是 utf-8
# 所以需要先将 gbk encode 成 gbk 才能 encode 为 utf-8
gbk_to_utf8 = s_to_gbk.decode(‘gbk‘).encode(‘utf-8‘)
print(‘utf-8‘, gbk_to_utf8)

  

编码 decode & encode

原文:https://www.cnblogs.com/PAYNE1Z/p/8966411.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!