该问题主要常见于Python2代码向Python3的移植过程中,因为Python3中是有bytes类型的,但在Python 2中它还是用string类型来表示。
# string to bytes
a = 'abcd'
print(a)
b = a.encode()
print(b)
###输出
abcd
b'abcd'
# bytes to string
a = b'abcd'
print(a)
b = a.decode()
print(b)
###输出
b'abcd'
abcd