W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
你想讀寫二進(jìn)制文件,比如圖片,聲音文件等等。
使用模式為 rb
或 wb
的 open()
函數(shù)來讀取或?qū)懭攵M(jìn)制數(shù)據(jù)。比如:
# Read the entire file as a single byte string
with open('somefile.bin', 'rb') as f:
data = f.read()
# Write binary data to a file
with open('somefile.bin', 'wb') as f:
f.write(b'Hello World')
在讀取二進(jìn)制數(shù)據(jù)時,需要指明的是所有返回的數(shù)據(jù)都是字節(jié)字符串格式的,而不是文本字符串。類似的,在寫入的時候,必須保證參數(shù)是以字節(jié)形式對外暴露數(shù)據(jù)的對象(比如字節(jié)字符串,字節(jié)數(shù)組對象等)。
在讀取二進(jìn)制數(shù)據(jù)的時候,字節(jié)字符串和文本字符串的語義差異可能會導(dǎo)致一個潛在的陷阱。特別需要注意的是,索引和迭代動作返回的是字節(jié)的值而不是字節(jié)字符串。比如:
>>> # Text string
>>> t = 'Hello World'
>>> t[0]
'H'
>>> for c in t:
... print(c)
...
H
e
l
l
o
...
>>> # Byte string
>>> b = b'Hello World'
>>> b[0]
72
>>> for c in b:
... print(c)
...
72
101
108
108
111
...
>>>
如果你想從二進(jìn)制模式的文件中讀取或?qū)懭胛谋緮?shù)據(jù),必須確保要進(jìn)行解碼和編碼操作。比如:
with open('somefile.bin', 'rb') as f:
data = f.read(16)
text = data.decode('utf-8')
with open('somefile.bin', 'wb') as f:
text = 'Hello World'
f.write(text.encode('utf-8'))
二進(jìn)制I/O還有一個鮮為人知的特性就是數(shù)組和C結(jié)構(gòu)體類型能直接被寫入,而不需要中間轉(zhuǎn)換為自己對象。比如:
import array
nums = array.array('i', [1, 2, 3, 4])
with open('data.bin','wb') as f:
f.write(nums)
這個適用于任何實現(xiàn)了被稱之為”緩沖接口”的對象,這種對象會直接暴露其底層的內(nèi)存緩沖區(qū)給能處理它的操作。二進(jìn)制數(shù)據(jù)的寫入就是這類操作之一。
很多對象還允許通過使用文件對象的 readinto()
方法直接讀取二進(jìn)制數(shù)據(jù)到其底層的內(nèi)存中去。比如:
>>> import array
>>> a = array.array('i', [0, 0, 0, 0, 0, 0, 0, 0])
>>> with open('data.bin', 'rb') as f:
... f.readinto(a)
...
16
>>> a
array('i', [1, 2, 3, 4, 0, 0, 0, 0])
>>>
但是使用這種技術(shù)的時候需要格外小心,因為它通常具有平臺相關(guān)性,并且可能會依賴字長和字節(jié)順序(高位優(yōu)先和低位優(yōu)先)。可以查看5.9小節(jié)中另外一個讀取二進(jìn)制數(shù)據(jù)到可修改緩沖區(qū)的例子。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: