Python3中內置類型bytes和str用法及byte和string之間各種編碼轉換


 

    Python 3最重要的新特性大概要算是對文本和二進制數據作了更為清晰的區分。文本總是Unicode,由str類型表示,二進制數據則由bytes類型表示。Python 3不會以任意隱式的方式混用str和bytes,正是這使得兩者的區分特別清晰。你不能拼接字符串和字節包,也無法在字節包里搜索字符串(反之亦然),也不能將字符串傳入參數為字節包的函數(反之亦然).

    python3.0中怎么創建bytes型數據

1
2
bytes([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ])
bytes( "python" 'ascii' # 字符串,編碼

設置一個原始的字符串

1
2
3
4
5
6
>>> website  =  'http://www.169it.com/os'
>>>  type (website)
< class  'str' >
>>> website
'http://www.169it.com/os'
>>>

按utf-8的方式編碼,轉成bytes

1
2
3
4
5
6
>>> website_bytes_utf8  =  website.encode(encoding = "utf-8" )
>>>  type (website_bytes_utf8)
< class  'bytes' >
>>> website_bytes_utf8
b 'http://www.169it.com/os'
>>>

  按gb2312的方式編碼,轉成bytes

1
2
3
4
5
6
>>> website_bytes_gb2312  =  website.encode(encoding = "gb2312" )
>>>  type (website_bytes_gb2312)
< class  'bytes' >
>>> website_bytes_gb2312
b 'http://www.169it.com/os'
>>>

   解碼成string,默認不填

1
2
3
4
5
6
7
>>> website_string  =  website_bytes_utf8.decode()
>>>  type (website_string)
< class  'str' >
>>> website_string
'http://www.169it.com/os'
>>>
>>>

    解碼成string,使用gb2312的方式

1
2
3
4
5
6
>>> website_string_gb2312  =  website_bytes_gb2312.decode( "gb2312" )
>>>  type (website_string_gb2312)
< class  'str' >
>>> website_string_gb2312
'http://www.169it.com/os'
>>>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM