【发布时间】:2020-10-19 10:48:55
【问题描述】:
我有一堆 Python 2.7 代码,我正在尝试使其单源代码与 Python 3 兼容,以帮助随着时间的推移从 2.7 迁移。我看到的最常见的问题是简单地将非unicode 内存内容写入磁盘。例如:
with io.open(some_path, 'w', encoding='utf-8') as the_file:
the_file.write(unicode(json.dumps(some_object, indent=2)))
with io.open(some_path, 'w', encoding='utf-8') as the_file:
the_file.write(unicode(yaml.dump(some_object, default_flow_style=False))) # From PyYAML
with io.open(some_path, 'w', encoding='utf-8') as the_file:
the_file.write(unicode(some_multiline_string)) # A simple string passed in, not explicitly marked up as unicode where it was declared
当然,unicode 的转换在 Python 3 下会失败,因为该类型不存在。如果我改变演员表,就像这样:
the_file.write(str(json.dumps(some_object, indent=2)))
然后它在 Python 3 中工作,但在 Python 2 下失败,因为 str 和 unicode 是不同的,而 file.write 需要一个 unicode 参数。虽然json.dumps 调用可以适应直接使用文件的json.dump 调用,但据我所知,yaml 转储调用不能。
理想情况下,有一种方法可以将所有正在写入的东西的类型强制转换为 file.write 想要的类型(unicode 字符串),但我找不到那是什么。我曾希望你总是能够将decode 各种形式的非unicode 字符串转换成unicode 字符串,但是Python 2 中的str 对象似乎没有decode 函数。
我发现的所有其他问题(在 Stack Overflow 和其他地方)和文档都给出了相互矛盾的建议,专注于缓冲区对象,或者只是就如何在一个版本的 Python 或另一个版本中执行此操作提供建议。我需要一个在 Python 2.7 和 3.x 中同样有效的解决方案,我希望有一个优雅的 Python 式解决方案,它不涉及在检测哪个版本正在使用的测试上进行分支。
【问题讨论】:
-
你考虑过使用
six吗? -
这当然是一种选择,因为我相信我们正在使用其他已经需要它的依赖项。
-
FWIW,最接近的问题 (stackoverflow.com/questions/49702626/…) 似乎问了同样的问题,但答案仅涵盖代码内 unicode 文字的情况。
-
Python 2
str确实有一个.decode方法。 Python 3str类型没有。 -
就像已经建议的那样,
six具有您需要的功能和包装器。您当然可以尝试自己重新发明轮子,但为什么不依赖一个广泛使用且经过良好测试的库呢?
标签: python python-3.x python-2.7 unicode