python - unicode python 3 等效项

标签 python python-3.x translation porting

我目前正在尝试将一段代码从 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 中的

常规字符串是 unicode。您必须在 Python 3 中不遗余力地获取字节串(非 Unicode)。

关于python - unicode python 3 等效项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35185378/

相关文章:

javascript - 为什么 JS 中的简单阶乘算法比 Python 或 R 中的快得多?

python - 如何将wav文件转换为JSON文件

python - 列表差异

python - 有没有办法遍历列表并将所有内容转换为整数?

javascript - 为什么 Transcrypt 编译在带有 : os. system ('python -m transcrypt -b -m -n <somePythonFile>.py' ) 行的 Python 脚本中不起作用?

php - 如何将 Symfony 翻译的句子传递给 twig?

python - 使用 Paramiko 执行交互式工具的命令并读取其输出

python - 使用正则表达式替换单词中间的双引号

python - 如何将收到的所有输入放入数据集中?

php - 使用 php 从网站/html 页面获取所有可翻译文本