python字符串str和字节数组相互转化方法详细介绍
来源:动视网
责编:小OO
时间:2020-11-27 14:25:16
python字符串str和字节数组相互转化方法详细介绍
实例如下:# bytes object b = b";example";# str object s = ";example";# str to bytes bytes(s.encoding = ";utf8";) # bytes to str str(b.encoding = ";utf-8";) # an alternative method # str to bytes str.encode(s) # bytes to str bytes.decode(b)。
导读实例如下:# bytes object b = b";example";# str object s = ";example";# str to bytes bytes(s.encoding = ";utf8";) # bytes to str str(b.encoding = ";utf-8";) # an alternative method # str to bytes str.encode(s) # bytes to str bytes.decode(b)。

下面小编就为大家带来一篇python字符串str和字节数组相互转化方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
实例如下:
# bytes object
b = b"example"
# str object
s = "example"
# str to bytes
bytes(s, encoding = "utf8")
# bytes to str
str(b, encoding = "utf-8")
# an alternative method
# str to bytes
str.encode(s)
# bytes to str
bytes.decode(b)
python字符串str和字节数组相互转化方法详细介绍
实例如下:# bytes object b = b";example";# str object s = ";example";# str to bytes bytes(s.encoding = ";utf8";) # bytes to str str(b.encoding = ";utf-8";) # an alternative method # str to bytes str.encode(s) # bytes to str bytes.decode(b)。