python 2.x编解码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 2.x编解码相关的知识,希望对你有一定的参考价值。

#-- coding:utf-8 -- #文件指定为utf-8编码 Python 2.x的程序默认编码为ascii
import sys
print(sys.getdefaultencoding()) #获取程序默认编码
s = u"你好"
#u代表unicode
print(s)
#s_to_unicode = s.decode("utf-8")
#print(s_to_unicode)
s_to_gbk = s.encode("gbk")
print(s_to_gbk)
#print("你好")
gbk_to_utf8 = s_to_gbk.decode("gbk").encode("utf8")
print(gbk_to_utf8)

以上是关于python 2.x编解码的主要内容,如果未能解决你的问题,请参考以下文章

编解码器无法在 Python 中解码字节 [重复]

Python JSON to CSV - 编码错误,UnicodeDecodeError:'charmap'编解码器无法解码字节

python3的base64编解码

Python/Flask:UnicodeDecodeError/UnicodeEncodeError:“ascii”编解码器无法解码/编码

Python 3.x 编解码

Python pandas to_excel'utf8'编解码器无法解码字节