Backporting Python 3 open(encoding=utf-8) to Python 2

Backporting Python 3 open(encoding=utf-8) to Python 2

1. To get an encoding parameter in Python 2:

If you only need to support Python 2.6 and 2.7 you can use io.open instead of open. io is the new io subsystem for Python 3, and it exists in Python 2,6 ans 2.7 as well. Please be aware that in Python 2.6 (as well as 3.0) its implemented purely in python and very slow, so if you need speed in reading files, its not a good option.

If you need speed, and you need to support Python 2.6 or earlier, you can use codecs.open instead. It also has an encoding parameter, and is quite similar to io.open except it handles line-endings differently.

2. To get a Python 3 open() style file handler which streams bytestrings:

open(filename, rb)

Note the b, meaning binary.

I think

from io import open

should do.

Backporting Python 3 open(encoding=utf-8) to Python 2

Heres one way:

with open(filename.txt, rb) as f:
    contents = f.read().decode(UTF-8)

Heres how to do the same thing when writing:

with open(filename.txt, wb) as f:
    f.write(contents.encode(UTF-8))

Leave a Reply

Your email address will not be published.