python基础:string和bytes数据类型之间的转换

一个很简单的问题,但是曾经因为这个很闹心了一会,把简单的解决方案记录在这里。

string = 'adidas NMD_XR1 Shoes - Grey | adidas UK'
print('type of string: ',type(string))
# string to bytes
new = string.encode(encoding='unicode-escape')

print('type of new: ',type(new))

# bytes to string
emm = string.encode('utf-8').decode('unicode_escape')

print('type of emm: ',type(emm))

output:

type of string: <class 'str'>
type of new: <class 'bytes'>
type of emm: <class 'str'>

猜你喜欢

转载自blog.csdn.net/ninnyyan/article/details/80564225