python byte和str转换

b = b'hello'
# byte转str
str1=str(b, encoding = "utf-8")  
str1=str(b, encoding = "gbk")  
str1=b.decode() # 第一参数默认utf8,第二参数默认strict
str1=b.decode('utf-8','ignore') # 忽略非法字符,用strict会抛出异常
str1=b.decode('utf-8','replace') # 用?取代非法字符

# str转byte
b=bytes(str1, encoding='utf-8')
b=str1.encode('utf-8')

参考:
https://blog.csdn.net/weixin_41733260/article/details/88713958

猜你喜欢

转载自blog.csdn.net/luo15242208310/article/details/112755050