Given the six.text_type
function. it's easy to write i/o code for unicode text, e.g. https://github.com/nltk/nltk/blob/develop/nltk/parse/malt.py#L188
fout.write(text_type(line))
But without the six
module, it would require a try-except
gymnastics that looks like this:
try:
fout.write(text_type(line))
except:
try:
fout.write(unicode(line))
except:
fout.write(bytes(line))
What is the pythonic way to resolve the file writing a unicode line and ensuring the python script is py2.x and py3.x compatible?
Is the try-except
above the pythonic way to handle the py2to3 compatibility? What other alternatives are there?
For more details/context of this question: https://github.com/nltk/nltk/issues/1080#issuecomment-134542174