- 获取的数据是bytes类型,需要做转换为str类型:
# 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)
本文介绍了如何在Python中将bytes类型的数据转换为str类型,并反过来将str类型转换为bytes类型。通过实例展示了两种主要的转换方法:使用encode()函数进行字符串到字节串的转换,以及使用decode()函数进行字节串到字符串的转换。
784

被折叠的 条评论
为什么被折叠?



