001_021 Python 在Unicode和普通如ANSI编码之间转化

来源:互联网 发布:手机上写代码的软件php 编辑:程序博客网 时间:2024/05/22 05:30

代码如下:

#encoding=utf-8print '中国'#在Unicode和普通如ANSI编码之间转化ustr=u'abc'ustr2=u'abc中国'#一 unicode 转换为普通字符串print ustr.encode('ascii') #print ustr2.encode('ascii')  # 这个失败  包含ascii表示不了的字符print ustr2.encode('utf-8') #二 普通字符串转换为unicodeprint unicode('abc')print 'abc中国'.decode('utf-8')print type('abc中国'.decode('utf-8'))

打印结果如下:

中国
abc
abc中国
abc
abc中国
<type 'unicode'>

0 0