Linux培训
达内IT学院
400-996-5531
python常用编码转换函数:
(1) unicode()
全局函数,将str字符串从其它编码(如gb2312)转换为unicode对象
(2) str.decode()
str字符串的方法,将str字符串从其它编码(如gb2312)转换为unicode对象,与unicode()很相似.
(3) str.encode()
str字符串的方法,
(4) unicode.encode()
unicode对象的方法,将unicode对象转成其它相应编码的str字符串对象,如gb2312.
(5) unicode.decode()
unicode对象的方法
应用例子:
>>> s="中文" #str对象
>>> ss=u"中文" #unicode对象
>>> type(s)
<type 'str'>
>>> type(ss)
<type 'unicode'>
>>> s1=unicode(s,'mbcs') # str对象 --> unicode对象
>>> s1
u'\u4e2d\u6587'
>>> type(s1)
<type 'unicode'>
>>> s3=ss.encode('mbcs') # unicode对象 --> str对象
>>> s3
'\xd6\xd0\xce\xc4'
>>> type(s3)
<type 'str'></P>
填写下面表单即可预约申请免费试听! 怕学不会?助教全程陪读,随时解惑!担心就业?一地学习,可全国推荐就业!
Copyright © Tedu.cn All Rights Reserved 京ICP备08000853号-56
京公网安备 11010802029508号 达内时代科技集团有限公司 版权所有