Search code examples
python-2.7python-3.xunicodeiosix

Writing py2.x and py3.x compatible code without six.text_type


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


Solution

  • Do what six does, and define text_type yourself:

    try:
        # Python 2
        text_type = unicode
    except NameError:
        # Python 3
        text_type = str
    

    In any case, never use blanked except lines here, you'll be masking other issues entirely unrelated to using a different Python version.

    It is not clear to me what kind of file object you are writing to however. If you are using io.open() to open the file you'll get a file object that'll always expect Unicode text, in both Python 2 and 3, and you should not need to convert text to bytes, ever.