【问题标题】:unicode python 3 equivalentunicode python 3等价物
【发布时间】:2016-05-13 03:23:56
【问题描述】:

我目前正在尝试将一段代码从 python 2 转换为 python 3,但我找不到 unicode 的 python 3 等效项:

class NavigableString(unicode_literals, PageElement):

    def toEncoding(self, s, encoding=None):
    """Encodes an object to a string in some encoding, or to Unicode.
    ."""
    if isinstance(s, unichr()):
        if encoding:
            s = s.encode(encoding)
    elif isinstance(s, str):
        if encoding:
            s = s.encode(encoding)
        else:
            s = unicode_literals(s)
    else:
        if encoding:
            s  = self.toEncoding(str(s), encoding)
        else:
            s = unicode_literals(s)
    return s

【问题讨论】:

  • ...也就是说,您在这里查看的所有代码在 Python 3 中都是不必要的,并且可以直接删除而不是翻译,除非您从字节串开始而不是常规的字符串。
  • 这可能会有所帮助。 how to make unicode strings with python 3

标签: python python-3.x translation porting


【解决方案1】:

常规字符串在 Python 3 中是 unicode。您必须在 Python 3 中竭尽全力获取字节串(非 Unicode)。

【讨论】:

    猜你喜欢
    • 2011-02-05
    • 1970-01-01
    • 2011-12-18
    • 2012-12-20
    • 1970-01-01
    • 1970-01-01
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多