给定six.text_type
功能。为 unicode 文本编写 i/o 代码很容易,例如https://github.com/nltk/nltk/blob/develop/nltk/parse/malt.py#L188
fout.write(text_type(line))
但是如果没有这个six
模块,它需要一个try-except
看起来像这样的体操:
try:
fout.write(text_type(line))
except:
try:
fout.write(unicode(line))
except:
fout.write(bytes(line))
什么是解决文件写入 unicode 行并确保 python 脚本兼容 py2.x 和 py3.x 的 pythonic 方法?
以上是try-except
处理py2to3兼容性的pythonic方式吗?还有哪些其他选择?
有关此问题的更多详细信息/上下文:https ://github.com/nltk/nltk/issues/1080#issuecomment-134542174