python str与bytes之间的转换
1 # bytes object 2 b = b"example" 3 4 # str object 5 s = "example" 6 7 # str to bytes 8 sb = bytes(s, encoding = "utf8") 9 10 # bytes to str 11 bs = str(b, encoding = "utf8") 12 13 # an alternative method 14 # str to bytes 15 sb2 = str.encode(s) 16 17 # bytes to str 18 bs2 = bytes.decode(b)
# bytes object
b = b"example"
# str object
s = "example"
# str to bytes
sb = bytes(s, encoding = "utf8")
# bytes to str
bs = str(b, encoding = "utf8")
# an alternative method
# str to bytes
sb2 = str.encode(s)
# bytes to str
bs2 = bytes.decode(b)
如果觉得这文章还算用心,请劳驾点击右下角的推荐,这是对我们这些做开源分享的最大的肯定,谢谢。
作者:zqifa
出处:https://www.cnblogs.com/zqifa/
欢迎访问新博客地址:https://www.l1mn.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。