如何基于与unthon'兼容的“unicode”制作逻辑

时间:2017-03-08 17:11:41

标签: python python-3.x unicode compatibility python-2.x

我想让这段代码与python2-3兼容:

def normalize_text(text, ignore_characters=''):
    if type(ignore_characters) not in [list, str, unicode]:
        ignore_characters = ''
    if type(ignore_characters) == str:
        ignore_characters = ignore_characters.decode('utf-8')
    if type(ignore_characters) == list:
        new_ignore_characters = []
        for item in ignore_characters:
            if type(item) == str:
                new_ignore_characters.append(item.decode('utf-8'))
            elif type(item) == unicode:
                new_ignore_characters.append(item)
        ignore_characters = new_ignore_characters

    if type(text) == str:
        text = text.decode('utf-8')

python 3中unicode类型上没有decodestr。使此代码python2-3兼容的最佳解决方法是什么?

1 个答案:

答案 0 :(得分:2)

我强烈建议使用six库编写兼容Python 2/3的代码。

另外使用isinstance()来检查类型而不是type()type()在多重继承的情况下不起作用:

from six import text_type, binary_type

if isinstance(ignore_characters, binary_type):
   # do something with bytes
elif isinstance(ignore_characters, text_type):
   # do something with unicode.
# Python 2 
>>> import six
>>> six.binary_type, six.text_type
(<type 'str'>, <type 'unicode'>)

# Python 3
>>> import six
>>> six.binary_type, six.text_type
(<class 'bytes'>, <class 'str'>)

其他方法是基于使用sys.version_info获得的Python版本基本编写自己的别名以实现兼容性:

这方面的一个很好的例子是来自compat.py库的requests文件:

_ver = sys.version_info

#: Python 2.x?
is_py2 = (_ver[0] == 2)

#: Python 3.x?
is_py3 = (_ver[0] == 3)

if is_py2:
    builtin_str = str
    bytes = str
    str = unicode
    basestring = basestring
    numeric_types = (int, long, float)
    integer_types = (int, long)

elif is_py3:
    builtin_str = str
    str = str
    bytes = bytes
    basestring = (str, bytes)
    numeric_types = (int, float)
    integer_types = (int,)

现在您可以从此文件导入这些函数,而不是直接使用内置函数。