美文网首页
Python之bytes和str类型转换

Python之bytes和str类型转换

作者: yytester | 来源:发表于2018-06-01 10:35 被阅读68次

  • 获取的数据是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类型转换

      本文链接:https://www.haomeiwen.com/subject/jlphsftx.html