统一采用一种编码形式

统一采用一种编码形式

  在编写Python程序的时候,一定要把编码和解码的操作放在外界来做。程序的核心部分应该使用Unicode字符类型,而且不要对字符的编码做任何设置。我们希望让用户无论输入的是str还是bytes类型,都保证返回统一的字符编码形式。

  这样做既可以令程序接收多种类型的文本编码,又可以保证输出的文本信息只采用一种编码形式,通过外界保证了编码的统一。

由于字符串类型有分别,所以Python代码中常常会出现两种常见的使用场景:    

1.开发者需要原始的8位bit值,这些8位bit值表示的是以UTF-8格式(或其他编码形式)来编码的字符。

2.开发者需要操作没有特定编码形式的Unicode字符。

  一 在Python3中,我们需要编写接收 str 或 bytes,并总是返回str的方法:

# 输入str或bytes都统一返回str
def to_str(str_or_bytes):
    if isinstance(str_or_bytes, bytes):

        # 如果是bytes,解码成str
        value = str_or_bytes.decode('utf-8')

    else:
        value = str_or_bytes

    return value

res = to_str(b'jason like shenghao!')
print(type(res))

>>> <class 'str'>

 

  总返回bytes的方法

# 输入str或bytes都统一返回bytes
def to_bytes(str_or_bytes):
    if isinstance(str_or_bytes, str):

        # 如果是str,编码成bytes
        value = str_or_bytes.encode('utf-8')

    else:
        value = str_or_bytes

    return value

res = to_bytes('jason 喜欢吃生蚝')
print(res)
print(type(res))

>>> b'jason \xe5\x96\x9c\xe6\xac\xa2\xe5\x90\x83\xe7\x94\x9f\xe8\x9a\x9d'
>>> <class 'bytes'>

 

 

二 在Python2中,我们需要编写接收 str 或 Unicode,并总是返回Unicode的方法:

'''
python2中
'''
# 输入str或unicode都统一返回unicode
def to_unicode(str_or_unicode):
    if isinstance(str_or_unicode, str):

        # 如果是unicode,编码成str
        value = str_or_unicode.encode('utf-8')

    else:
        value = str_or_unicode

    return value

 总返回str

# 输入str或unicode都统一返回str
def to_str(str_or_unicode):
    if isinstance(str_or_unicode, unicode):

        # 如果是bytes,解码成str
        value = str_or_unicode.decode('utf-8')

    else:
        value = str_or_unicode

    return value

 

总结: 对输入的数据进行操作之前,使用外界的辅助函数来保证字符序列的类型与开发者期望的相符合。

 

posted @ 2019-07-10 10:11  tank_jam  阅读(298)  评论(0编辑  收藏  举报