
Python编码类型转换方法,分享给大家参考,具体如下:
Python和unicode
Python在2.0版后引入Unicode字符串,以正确处理多语言文本。
推荐:Python教程
printthonprint
虽然python需要将文本编码转换为unicode编码进行处理,但终端显示工作由传统的python字符串完成(事实上,pythonprint
双字节Unicode编码字符根本无法打印语句。
python的print将自动转换输出的unicode编码(其他非unicode编码,print将原始输出)(输出到控制台时),文件对象
write方法不会做,所以当一些字符串用print输出正常时,write到文件可能不一定和print一样。
在linux下,根据环境变量进行转换,在linux下使用locale命令可以看到。print语句的实现是将要输出的内容传输到操作系统和操作系统
系统将根据系统编码编码输入的字节流。
>>>str='学习python#39; >>>str '\xe5\xadxa6\xe4\xb9\xapython'#ASII编码 >>>printstr
python中的decode将其它字符集转换为unicode编码(只有中文字符需要转换)
>>>str='学习'
>>>ustr=str.decode('utf-8')
>>>ustr
u'\u5b66\u4e60#;python中的encodedede
将unicode转换为其他字符
>>>str='学习'
>>>ustr=str.decode('utf-8')
>>>ustr
u'\u5b66\u4e60#;更多技术请关注Python视频教程。