欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python3中內(nèi)置類型bytes和str用法及byte和string之間各種編碼轉(zhuǎn)換 問題

 更新時間:2018年09月27日 09:20:17   作者:johnny1024  
這篇文章主要介紹了Python3中內(nèi)置類型bytes和str用法及byte和string之間各種編碼轉(zhuǎn)換問題,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下

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

    python3.0中怎么創(chuàng)建bytes型數(shù)據(jù)

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

設(shè)置一個原始的字符串

>>> website = 'http://www.169it.com/os'
>>> type(website)
<class 'str'>
>>> website
'http://www.169it.com/os'
>>>

按utf-8的方式編碼,轉(zhuǎn)成bytes

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

  按gb2312的方式編碼,轉(zhuǎn)成bytes

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

   解碼成string,默認(rèn)不填

>>> website_string = website_bytes_utf8.decode()
>>> type(website_string)
<class 'str'>
>>> website_string
'http://www.169it.com/os'
>>>
>>>

   解碼成string,使用gb2312的方式

>>> website_string_gb2312 = website_bytes_gb2312.decode("gb2312")
>>> type(website_string_gb2312)
<class 'str'>
>>> website_string_gb2312
'http://www.169it.com/os'
>>> 

總結(jié)

以上所述是小編給大家介紹的Python3中內(nèi)置類型bytes和str用法及byte和string之間各種編碼轉(zhuǎn)換 問題,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論