Python3中的bytes和str類型詳解

這篇文章主要介紹了Python3中的bytes和str類型,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨着小編來一起學習學習吧

Python 3最重要的新特性之一是對字符串和二進制數據流做了明確的區分。文本總是Unicode,由str類型表示,二進制數據則由bytes類型表示。Python 3不會以任意隱式的方式混用str和bytes,你不能拼接字符串和字節流,也無法在字節流裏搜索字符串(反之亦然),也不能將字符串傳入參數爲字節流的函數(反之亦然)。

下面讓我們深入分析一下二者的區別和聯繫。

編碼發展的歷史

在談bytes和str之前,需要先說說關於編碼是如何發展的。。

在計算機歷史的早期,美國爲代表的英語系國家主導了整個計算機行業,26個英文字母組成了多樣的英語單詞、語句、文章。因此,最早的字符編碼規範是ASCII碼,一種8位即1個字節的編碼規範,它可以涵蓋整個英語系的編碼需要。

編碼是什麼?編碼就是把一個字符用一個二進制來表示。我們都知道,所有的東西,不管是英文、中文還是符號等等,最終存儲在磁盤上都是01010101這類東西。在計算機內部,讀取和存儲數據歸根結底,處理的都是0和1組成的比特流。問題來了,人類看不懂這些比特流,如何讓這些010101對人類變得可讀呢?於是出現了字符編碼,它是個翻譯機,在計算機內部某個地方,透明的幫我們將比特流翻譯成人類可以直接理解的文字。對於一般用戶,不需要知道這個過程是什麼原理,是怎麼執行的。但是對於程序員卻是個必須搞清楚的問題。

以ASCII編碼爲例,它規定1個字節8個比特位代表1個字符的編碼,也就是“00000000”這麼寬,一個一個字節的解讀。例如:01000001表示大寫字母A,有時我們會“偷懶"的用65這個十進制來表示A在ASCII中的編碼。8個比特位,可以沒有重複的最多表示2的8次方(255)個字符。

後來,計算機得到普及,中文、日文、韓文等等國家的文字需要在計算機內表示,ASCII的255位遠遠不夠,於是標準組織制定出了叫做UNICODE的萬國碼,它規定任何一個字符(不管哪國的)至少以2個字節表示,可以更多。其中,英文字母就是用2個字節,而漢字是3個字節。這個編碼雖然很好,滿足了所有人的要求,但是它不兼容ASCII,同時還佔用較多的空間和內存。因爲,在計算機世界更多的字符是英文字母,明明可以1個字節就能夠表示,非要用2個。

於是UTF-8編碼應運而生,它規定英文字母系列用1個字節表示,漢字用3個字節表示等等。因此,它兼容ASCII,可以解碼早期的文檔。UTF-8很快就得到了廣泛的應用。

在編碼的發展歷程中,我國還創造了自己的編碼方式,例如GBK,GB2312,BIG5。他們只侷限於在國內使用,不被國外認可。在GBK編碼中,中文漢字佔2個字節。

bytes和str之間的異同

回到bytes和str的身上。bytes是一種比特流,它的存在形式是01010001110這種。我們無論是在寫代碼,還是閱讀文章的過程中,肯定不會有人直接閱讀這種比特流,它必須有一個編碼方式,使得它變成有意義的比特流,而不是一堆晦澀難懂的01組合。因爲編碼方式的不同,對這個比特流的解讀也會不同,對實際使用造成了很大的困擾。下面讓我們看看Python是如何處理這一系列編碼問題的:

>>> s = "中文"
>>> s
'中文'
>>> type(s)
<class 'str'>
>>> b = bytes(s, encoding='utf-8')
>>> b
b'\xe4\xb8\xad\xe6\x96\x87'   #\x 代表是十六進制
>>> type(b)
<class 'bytes'>

從例子可以看出,s是個字符串類型。Python有個內置函數bytes()可以將字符串str類型轉換成bytes類型,b實際上是一串01的組合,但爲了在ide環境中讓我們相對直觀的觀察,它被表現成了b'\xe4\xb8\xad\xe6\x96\x87'這種形式,開頭的b表示這是一個bytes類型。\xe4是十六進制的表示方式,它佔用1個字節的長度,因此”中文“被編碼成utf-8後,我們可以數得出一共用了6個字節,每個漢字佔用3個,這印證了上面的論述。在使用內置函數bytes()的時候,必須明確encoding的參數,不可省略。

我們都知道,字符串類str裏有一個encode()方法,它是從字符串向比特流的編碼過程。而bytes類型恰好有個decode()方法,它是從比特流向字符串解碼的過程。除此之外,我們查看Python源碼會發現bytes和str擁有幾乎一模一樣的方法列表,最大的區別就是encode和decode。

從實質上來說,字符串在磁盤上的保存形式也是01的組合,也需要編碼解碼。

如果,上面的闡述還不能讓你搞清楚兩者的區別,那麼記住下面兩幾句話:

在將字符串存入磁盤和從磁盤讀取字符串的過程中,Python自動地幫你完成了編碼和解碼的工作,你不需要關心它的過程。

使用bytes類型,實質上是告訴Python,不需要它幫你自動地完成編碼和解碼的工作,而是用戶自己手動進行,並指定編碼格式。

Python已經嚴格區分了bytes和str兩種數據類型,你不能在需要bytes類型參數的時候使用str參數,反之亦然。這點在讀寫磁盤文件時容易碰到。

在bytes和str的互相轉換過程中,實際就是編碼解碼的過程,必須顯式地指定編碼格式。

>>> b
b'\xe4\xb8\xad\xe6\x96\x87'
>>> type(b)
<class 'bytes'>
>>> s1 = str(b)
>>> s1
"b'\\xe4\\xb8\\xad\\xe6\\x96\\x87'"  #注意這裏
>>> type(s1)
<class 'str'>
>>> s1 = str(b, encoding='utf-8')
>>> s1
'中文'
>>> type(s1)
<class 'str'>

我們再把字符串s1,轉換成gbk編碼的bytes類型:

>>> s1
'中文'
>>> type(s1)
<class 'str'>
>>> b = bytes(s1, encoding='gbk')
>>> b

編碼可以將抽象字符以二進制數據的形式表示,有很多編碼方法,如utf-8、gbk等,可以使用encode()函數對字符串進行編碼,轉換成二進制字節數據,也可用decode()函數將字節解碼成字符串;用decode()函數解碼,可不要用指定編碼格式;

>>> a = 'hello world'
>>> type(a)
<class 'str'>
>>> a
'hello world'

a. 按utf-8的方式編碼,轉成bytes:以及解碼成字符串

#a爲串'hello world'
>>> b = a.encode(encoding='utf-8')
>>> type(b)
<class 'bytes'>
>>>
>>> b
b'hello world'
>>>
>>>
>>> c = b.decode(encoding='utf-8')
>>> type(c)
<class 'str'>
>>> c
'hello world'

b. 按gbk的方式編碼,轉成bytes:以及解碼成字符串

path="doc.txt"
 
f=open(path,'rb')
 
raw=f.read()
print(raw[:100])
print(len(raw))
print(type(raw))
 
raw1=str(raw)
print(raw1[:100])
print(len(raw1))
print(type(raw1))
 
tokens=word_tokenize(raw1)
print(len(tokens))
print(tokens[:20])
b'the rock is destined to be the 21st century\'s new " conan " and that he\'s going to make a splash eve'
626168
<class 'bytes'>
b'the rock is destined to be the 21st century\'s new " conan " and that he\'s going to make a splash
634857
<class 'str'>
115104
["b'the", 'rock', 'is', 'destined', 'to', 'be', 'the', '21st', 'century\\', "'s", 'new', '``', 'conan', '``', 'and', 'that', 'he\\', "'s", 'going', 'to']
from nltk import word_tokenize
 
path="doc.txt" 
 
f=open(path,'rb') 
 
raw=f.read() 
print(raw[:1000]) 
print(len(raw)) 
print(type(raw)) 
raw=raw[:1000]
 
 
raw1= raw.decode(encoding='utf-8') #在4000多的位置上有無法解碼的,也就是說解碼方式是有問題的,應該不是utf-8
print(raw1[:1000]) 
print(len(raw1)) 
print(type(raw1)) 
b'the rock is destined to be the 21st century\'s new " conan " and that he\'s going to make a splash even greater than arnold schwarzenegger , jean-claud van damme or steven segal . \nthe gorgeously elaborate continuation of " the lord of the rings " trilogy is so huge that a column of words cannot adequately describe co-writer/director peter jackson\'s expanded vision of j . r . r . tolkien\'s middle-earth . \neffective but too-tepid biopic\nif you sometimes like to go to the movies to have fun , wasabi is a good place to start . \nemerges as something rare , an issue movie that\'s so honest and keenly observed that it doesn\'t feel like one . \nthe film provides some great insight into the neurotic mindset of all comics -- even those who have reached the absolute top of the game . \noffers that rare combination of entertainment and education . \nperhaps no picture ever made has more literally showed that the road to hell is paved with good intentions . \nsteers turns in a snappy screenplay that curl'
626168
<class 'bytes'>
the rock is destined to be the 21st century's new " conan " and that he's going to make a splash even greater than arnold schwarzenegger , jean-claud van damme or steven segal . 
the gorgeously elaborate continuation of " the lord of the rings " trilogy is so huge that a column of words cannot adequately describe co-writer/director peter jackson's expanded vision of j . r . r . tolkien's middle-earth . 
effective but too-tepid biopic
if you sometimes like to go to the movies to have fun , wasabi is a good place to start . 
emerges as something rare , an issue movie that's so honest and keenly observed that it doesn't feel like one . 
the film provides some great insight into the neurotic mindset of all comics -- even those who have reached the absolute top of the game . 
offers that rare combination of entertainment and education . 
perhaps no picture ever made has more literally showed that the road to hell is paved with good intentions . 
steers turns in a snappy screenplay that curl
1000
<class 'str'>

189
['e', 'rock', 'is', 'destined', 'to', 'be', 'the', '21st', 'century', "'s", 'new', '``', 'conan', '``', 'and', 'that', 'he', "'s", 'going', 'to']

將a(字符串)進行編碼,當a爲中文時,將Unicode編碼爲b'\\u4f60\\u597d',輸入b顯示時,python自動假設unicode字符的默認編碼方式是ASCII,但ASCII中沒有b'\\u4f60\\u597d',所以就直接輸出了。

當a爲'hello'時,編碼爲(也是數字),但print時可以按照ASCII的形式顯示bytes類型。

>>>a='你好'
>>>b=a.encode('unicode_escape')
>>>b
b'\\u4f60\\u597d'
 
 
>>>a='hello'
>>>b=a.encode('unicode_escape')
>>>b
b'hello'

以上所述是小編給大家介紹的Python3中的bytes和str類型詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對神馬文庫網站的支持!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章