python2和python3的转换
2024-04-09 22:00:59  阅读数 2105

1. python2 程序转换为python3

使用python3自带的程序2to3。 若是安装包安装python3,则2to3在Tools文件夹中,若使用conda安装python3,则在bin目录下。

2to3 -w python2/ -n -o python3/

2. python3和python2常见的报错

TypeError: a bytes-like object is required, not 'str'

python bytes和str两种类型可以通过函数encode()和decode()相互转换

str    --->>> bytes      encode()

bytes ---->>>str      decode()

line.strip().split()  ---->> line.decode().strip().split()