【发布时间】:2015-08-25 10:20:30
【问题描述】:
给定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
【问题讨论】:
标签: python-2.7 python-3.x unicode io six