Memo of troubles about coexistence of Python 2/3 system

Python 2.x-> Python 3.x I sometimes add some troubles in writing migration and compatibility code as my notes.

There is no encoding in the argument of the standard open function in Python 2.7

The encoding of the argument of the open function can be used in the Python 3 series, but it is not in the Python 2 series.

python


# OK on Python 3.x, NG on Python 2.7
with open("some_file_with_multibyte_char", "r", encoding="utf-8") as f:
    print(f.read())

To do the same in Python 2 series, open the file in binary mode and decode the contents, or

python


# OK on Python 2.7 and OK on Python 3.x
with open("some_file_with_multibyte_char", "rb") as f:
    print(f.read().decode(encoding="utf-8"))

Do you use open in the io module?

python


from io import open
# OK on both Python 3.x and Python 2.7
with open("some_file_with_multibyte_char", "r", encoding="utf-8") as f:
    print(f.read())

In Python 3.x, io.open is an alias for built-in open, so it seems better to use io.open in Python 2 series.

Postscript:

codecs.open is also Python 2/3 We received a comment that it is compatible. Thank you very much.




#### **`python`**
```python

import codecs
# OK on both Python 3.x and Python 2.7
with codecs.open('some_file_with_multibyte_char', 'r', 'utf-8') as f:
    print(f.read())

Recommended Posts

Memo of troubles about coexistence of Python 2/3 system
[Python] Memo about functions
About various encodings of Python 3
About the features of Python
[Python] Operation memo of pandas DataFrame
Python3 compatible memo of "python start book"
Basic grammar of Python3 system (dictionary)
Separate display of Python graphs (memo)
Coexistence of Python2 and 3 with CircleCI (1.0)
About the basics list of Python basics
python memo
Python memo
python memo
Python memo
Python memo
About building GUI using TKinter of Python
[Learning memo] Basics of class by python
About the virtual environment of python version 3.7
"System trade starting with Python3" reading memo
Memorandum of python beginners About inclusion notation
Basic grammar of Python3 system (character string)
[Python] Chapter 02-04 Basics of Python Program (About Comments)
Basic grammar of Python3 system (included notation)
Status of each Python processing system in 2020
About python slices
A memo of a tutorial on running python on heroku
[Python] Memo dictionary
About python comprehension
Introduction of Python
python beginner memo (9.2-10)
About Python tqdm.
About python yield
Python & Machine Learning Study Memo ⑤: Classification of irises
About python, class
python beginner memo (9.1)
Python & Machine Learning Study Memo ②: Introduction of Library
A memo about writing merge sort in Python
About python inheritance
Basics of Python ①
★ Memo ★ Python Iroha
Basics of python ①
A note about the python version of python virtualenv
About python, range ()
[Python] EDA memo
About python decorators
[Note] About the role of underscore "_" in Python
Python3 metaclass memo
[Python] Basemap memo
About python reference
About Python decorators
[Python] About multi-process
About the * (asterisk) argument of python (and itertools.starmap)
About shallow and deep copies of Python / Ruby
Python beginner memo (2)
[Python] Numpy memo
Introduction of Python
Get the update date of the Python memo file.
Implementation example of simple LISP processing system (Python version)
A memo connected to HiveServer2 of EMR with python
The pain of gRPC using Python. November 2019. (Personal memo)
A memo about building a Django (Python) application with Docker