1

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

TuringTux
  • 559
  • 1
  • 12
  • 26
alvas
  • 115,346
  • 109
  • 446
  • 738

1 Answers1

2

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Ingenious fix!!! Noted on the `bytes` conversion, I'll check how/why the bytes was used. – alvas Aug 25 '15 at 10:34