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)  

 

posted @ 2018-01-13 22:15  来呀快活吧  阅读(528)  评论(0编辑  收藏  举报
cs