python str与bytes之间的转换

  1. # bytes object  
  2.  b = b"example"  
  3.   
  4.  # str object  
  5.  s = "example"  
  6.   
  7.  # str to bytes  
  8.  bytes(s, encoding = "utf8")  
  9.   
  10.  # bytes to str  
  11.  str(b, encoding = "utf-8")  
  12.   
  13.  # an alternative method  
  14.  # str to bytes  
  15.  str.encode(s)  
  16.   
  17.  # bytes to str  
  18.  bytes.decode(b)  

猜你喜欢

转载自blog.csdn.net/golddaniu/article/details/80493194
今日推荐