怎么使用Python读写二进制文件
1. 简介
python 读写文件的二进制数据需要使用到struct模块,进行c/c++与python数据格式的转换。
2. struct模块介绍
struct模块中最常用的函数为pack和unpack,用法如下:
函数 | return | explain |
---|---|---|
pack(fmt,v1,v2…) | string | 按照给定的格式(fmt),把数据转换成字符串(字节流),并将该字符串返回. |
pack_into(fmt,buffer,offset,v1,v2…) | None | 按照给定的格式(fmt),将数据转换成字符串(字节流),并将字节流写入以offset开始的buffer中.(buffer为可写的缓冲区,可用array模块) |
unpack(fmt,v1,v2……) | tuple | 按照给定的格式(fmt)解析字节流,并返回解析结果 |
pack_from(fmt,buffer,offset) | tuple | 按照给定的格式(fmt)解析以offset开始的缓冲区,并返回解析结果 |
calcsize(fmt) | size of fmt | 计算给定的格式(fmt)占用多少字节的内存,注意对齐方式 |
3. struct模块中数据格式fmt对应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 |
4. 实例
注意:代码中,表示大端
import struct# 打开文件with open("binary_file.bin", "wb") as f: # 写入4个字节的整数(值为12345) int_value = 12345 f.write(struct.pack("<i with><h3>5. Python 字符串前面加u,r,b,f的含义</h3><h4>5.1. 字符串前加u</h4></i><p>后面字符串以 Unicode格式进行编码,一般用在中文字符串前面,防止因为源码储存格式问题,导致再次使用时出现乱码。</p><pre class="brush:py;">str= u'hello'
5.2. 字符串前加r
去掉反斜杠的转移机制。(特殊字符:即那些,反斜杠加上对应字母,表示对应的特殊含义的,比如最常见的””表示换行,” ”表示Tab等。 )
立即学习“Python免费学习笔记(深入)”;
str= r'hello'
5.3. 字符串前加b
表示该字符串是bytes 类型。
bytes = b'hello'
在 Python3 中,bytes 和 str 的互相转换方式是
str.encode(‘utf-8')bytes.decode(‘utf-8')
5.4. 字符串前加f
以 f 开头表示在字符串内支持大括号内的python 表达式,字符串拼接
name = 'Lily'print(f'My name is {name}.')