Python中bytes和str的區(qū)別與聯(lián)系詳解
Bytes和Str的區(qū)別
在Python3中,字符序列有兩種類型:bytes和str。bytes類型是無符號(hào)的8位值(通常以ASCII碼顯式),而str類型是Unicode代碼點(diǎn)(code point)。代碼點(diǎn)指編碼字符集中,字符所對(duì)應(yīng)的數(shù)字。
a = b'hello world' print(isinstance(a, bytes)) print(list(a)) print(a) """ True [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100] b'hello world' """ a = 'hello world' print(isinstance(a, str)) print(list(a)) print(a) """ True ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'] hello world """
isinstance()方法可以判斷對(duì)象的類型,例如這里用來判斷是str還是bytes。
Python3對(duì)文本(str)和二進(jìn)制數(shù)據(jù)(bytes)有著嚴(yán)格的區(qū)分,不能混用。
x = b'python' y = b'java' z = 'c++' w = 'c' print(x + y) # b'pythonjava' print(z + w) # c++c print(x + z) # TypeError: can't concat str to bytes print('python' == b'python') # False
上述示例中str類型和bytes類型間使用=來比較是否相等不會(huì)報(bào)錯(cuò),但是會(huì)返回False。
Bytes與Str間的轉(zhuǎn)換
str類型和bytes類型間可以相互轉(zhuǎn)換。
str到bytes的轉(zhuǎn)換需要調(diào)用encode()方法。
bytes到str間的轉(zhuǎn)換需要調(diào)用decode()方法。
x = b'python' y = x.decode(encoding='utf-8') z = y.encode(encoding='utf-8') print(y) print(z) """ python b'python' """
可以觀察到encode()和decode()方法都有一個(gè)encoding參數(shù)用來指定具體的編碼規(guī)則。
讀寫文件的注意事項(xiàng)
當(dāng)要將bytes類型寫入到文件中時(shí),必須指定mode=wb。讀取二進(jìn)制文件時(shí)可以指定mode=rb或者指定編碼方式,使用后者時(shí)讀出來的就不是bytes類型的字符序列了。
x = b'python' # 錯(cuò)誤示例 with open('data.bin', mode='w') as fp: fp.write(x) # TypeError: write() argument must be str, not bytes # 正確示例 with open('data.bin', mode='wb') as fp: fp.write(x) # 讀取二進(jìn)制文件方式1 with open('data.bin', mode='rb') as fp: content = fp.read() print(content) # python # 讀取二進(jìn)制文件方式2 with open('data.bin', mode='r', encoding='utf-8') as fp: content = fp.read() print(content, type(content)) # python <class 'str'>
當(dāng)讀寫Unicode數(shù)據(jù)時(shí),只需要注意下編碼方式即可,最好是顯式的傳遞encoding參數(shù)。
x = '世界你好' with open('data.txt', mode='w', encoding='utf-8') as fp: fp.write(x) with open('data.txt', mode='r', encoding='utf-8') as fp: content = fp.read() print(content) # 世界你好 # 錯(cuò)誤示例,編碼方式不對(duì) with open('data.txt', mode='r', encoding='gbk') as fp: content = fp.read() print(content) # 涓栫晫浣犲ソ
總結(jié)
到此這篇關(guān)于Python中bytes和str區(qū)別與聯(lián)系的文章就介紹到這了,更多相關(guān)Python bytes和str區(qū)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python2.7基于笛卡爾積算法實(shí)現(xiàn)N個(gè)數(shù)組的排列組合運(yùn)算示例
這篇文章主要介紹了Python2.7基于笛卡爾積算法實(shí)現(xiàn)N個(gè)數(shù)組的排列組合運(yùn)算,涉及Python笛卡爾積算法及排列組合操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-11-11Python 獲取numpy.array索引值的實(shí)例
今天小編就為大家分享一篇Python 獲取numpy.array索引值的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12python中計(jì)算一個(gè)列表中連續(xù)相同的元素個(gè)數(shù)方法
今天小編就為大家分享一篇python中計(jì)算一個(gè)列表中連續(xù)相同的元素個(gè)數(shù)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06Numpy中np.dot與np.matmul的區(qū)別詳解
本文主要介紹了Numpy中np.dot與np.matmul的區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02Django集成celery發(fā)送異步郵件實(shí)例
今天小編就為大家分享一篇Django集成celery發(fā)送異步郵件實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12Python 實(shí)現(xiàn)3種回歸模型(Linear Regression,Lasso,Ridge)的示例
這篇文章主要介紹了Python 實(shí)現(xiàn) 3 種回歸模型(Linear Regression,Lasso,Ridge)的示例,幫助大家更好的進(jìn)行機(jī)器學(xué)習(xí),感興趣的朋友可以了解下2020-10-10