码迷,mamicode.com
首页 > 编程语言 > 详细

Python2 处理 Unicode 字符串的规则

时间:2017-11-16 23:53:17      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:ack   byte   print   函数   unicode   read   python2   字符串   编解码   

在 Python2 中处理 Unicode 字符串,需遵循如下规则:

1. 程序中的字符串要加前缀 u

2. 不要用 str(),而应该用 unicode() 作为字符串转换函数。不要使用 chr(),而应该使用 unichr()

3. 不要使用 string 模块

4. 如非必要,不要使用 encode 和 decode 编解码 unicode 字符串。只有当要将 unicode 字符串写入文件,数据库,或者网络时,要先将其 encode 为 byte stream,然后再写入,同样的,从文件,数据库,或者网络读取的 byte stream 要先 decode 为 unicode 字符串,然后才能使用。

例如:

if __name__ == ‘__main__‘:

    #str_out = u‘Hello world!‘
    str_out = u‘宁静致远‘
    print ‘>>> str_out = %s‘ % str_out  # for test

    byte_encode = str_out.encode(‘utf-8‘)

    # write the encoded bytes stream into file
    fho = open(‘use_unicode_encode_decode_3.log‘, ‘w‘)
    fho.write(byte_encode)
    fho.close()

    # read the encoded bytes stream from file, and then decode it
    fhi = open(‘use_unicode_encode_decode_3.log‘, ‘r‘)
    byte_in = fhi.read()
    fhi.close()

    str_in = byte_in.decode(‘utf-8‘)
    print ‘<<< str_in = %s‘ % str_in  # for test


完。

 

Python2 处理 Unicode 字符串的规则

标签:ack   byte   print   函数   unicode   read   python2   字符串   编解码   

原文地址:http://www.cnblogs.com/gaowengang/p/7846815.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!