1. 簡介
Python 讀寫文件的二進(jìn)制數(shù)據(jù)需要使用到struct模塊,進(jìn)行C/C++與Python數(shù)據(jù)格式的轉(zhuǎn)換。
2. struct模塊介紹
struct模塊中最常用的函數(shù)為pack和unpack,用法如下:
函數(shù) | return | explain |
---|---|---|
pack(fmt,v1,v2…) | string | 按照給定的格式(fmt),把數(shù)據(jù)轉(zhuǎn)換成字符串(字節(jié)流),并將該字符串返回. |
pack_into(fmt,buffer,offset,v1,v2…) | None | 按照給定的格式(fmt),將數(shù)據(jù)轉(zhuǎn)換成字符串(字節(jié)流),并將字節(jié)流寫入以offset開始的buffer中.(buffer為可寫的緩沖區(qū),可用array模塊) |
unpack(fmt,v1,v2……) | tuple | 按照給定的格式(fmt)解析字節(jié)流,并返回解析結(jié)果 |
pack_from(fmt,buffer,offset) | tuple | 按照給定的格式(fmt)解析以offset開始的緩沖區(qū),并返回解析結(jié)果 |
calcsize(fmt) | size of fmt | 計(jì)算給定的格式(fmt)占用多少字節(jié)的內(nèi)存,注意對齊方式 |
3. struct模塊中數(shù)據(jù)格式fmt對應(yīng)C/C++和Python中的類型
Format | C Type | Python type | Standard size |
---|---|---|---|
x | pad byte | no value | |
c | char | string of length | 1 |
b | signed char | integer | 1 |
B | unsigned char | integer | 1 |
? | _Bool | bool | 1 |
h | short | integer | 2 |
H | unsigned short | integer | 2 |
i | int | integer | 4 |
I | unsigned int | integer | 4 |
l | long | integer | 4 |
L | unsigned long | integer | 4 |
q | long long | integer | 8 |
Q | unsigned long long | integer | 8 |
f | float | float | 4 |
d | double | float | 8 |
s | char[] | string | |
p | char[] | string | |
P | void * | integer |
5. 實(shí)例
注意:代碼中,<表示小端,>表示大端
import struct
# 打開文件
with open("binary_file.bin", "wb") as f:
# 寫入4個(gè)字節(jié)的整數(shù)(值為12345)
int_value = 12345
f.write(struct.pack("<i", int_value))
# 寫入8個(gè)字節(jié)的雙精度浮點(diǎn)數(shù)(值為3.14159)
double_value = 3.14159
f.write(struct.pack("<d", double_value))
# 寫入一個(gè)字節(jié)的布爾值(值為True)
bool_value = True
f.write(struct.pack("<?", bool_value))
# 寫入一個(gè)定長字符串(10個(gè)字符,值為"hello")
string_value = "hello".encode("utf-8")
f.write(struct.pack("<5s", string_value))
# 寫入一個(gè)定長字節(jié)數(shù)組(20個(gè)字節(jié),值為b"\x01\x02\x03...\x14")
byte_array_value = bytes(range(1, 21))
f.write(struct.pack("<20s", byte_array_value))
f.close()
# 打開文件
with open("binary_file.bin", "rb") as f:
# 讀取4個(gè)字節(jié),解析成一個(gè)整數(shù)
int_value = struct.unpack("<i", f.read(4))[0]
# 讀取8個(gè)字節(jié),解析成一個(gè)雙精度浮點(diǎn)數(shù)
double_value = struct.unpack("<d", f.read(8))[0]
# 讀取一個(gè)字節(jié),解析成一個(gè)布爾值
bool_value = struct.unpack("<?", f.read(1))[0]
# 讀取一個(gè)字符串,解析成一個(gè)定長字符串(10個(gè)字符)
string_value = struct.unpack("<5s", f.read(5))[0].decode("utf-8")
# 讀取一個(gè)字節(jié)數(shù)組,解析成一個(gè)定長字節(jié)數(shù)組(20個(gè)字節(jié))
byte_array_value = struct.unpack("<20s", f.read(20))[0]
# 打印結(jié)果
print(f"int_value: {int_value}")
print(f"double_value: {double_value}")
print(f"bool_value: {bool_value}")
print(f"string_value: {string_value}")
print(f"byte_array_value: {byte_array_value}")
f.close()
6. Python 字符串前面加u,r,b,f的含義
6.1. 字符串前加u
后面字符串以 Unicode格式進(jìn)行編碼,一般用在中文字符串前面,防止因?yàn)樵创a儲存格式問題,導(dǎo)致再次使用時(shí)出現(xiàn)亂碼。
str= u'hello'
6.2. 字符串前加r
去掉反斜杠的轉(zhuǎn)移機(jī)制。(特殊字符:即那些,反斜杠加上對應(yīng)字母,表示對應(yīng)的特殊含義的,比如最常見的”\n”表示換行,”\t”表示Tab等。 )
str= r'hello\n\t\n'
6.3. 字符串前加b
表示該字符串是bytes 類型。
bytes = b'hello'
在 Python3 中,bytes 和 str 的互相轉(zhuǎn)換方式是
str.encode(‘utf-8’)
bytes.decode(‘utf-8’)
6.4. 字符串前加f
以 f 開頭表示在字符串內(nèi)支持大括號內(nèi)的python 表達(dá)式,字符串拼接文章來源:http://www.zghlxwxcb.cn/news/detail-482978.html
name = 'Lily'
print(f'My name is {name}.')
參考
[1] python3中的struct模塊使用
[2] Python 字符串前面加u,r,b,f的含義文章來源地址http://www.zghlxwxcb.cn/news/detail-482978.html
到了這里,關(guān)于Python讀寫二進(jìn)制文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!