[Python]_[初级]_[使用struct库对二进制文件进行读写]

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/infoworld/article/details/81033276

场景

1.要分析或生成一个二进制文件时, Python使用 struct库来转换或分析二进制数据. 当然使用C++或者Java来分析也可以, 但对于脚本语言Python来说, 编写数据和调试时间都没Python快. 所以Python其实是比较正确的选择.

2.如果需要传输socket数据, 在定义好数据格式时自然也是使用stuct.pack和unpack来打包数据和分析数据.

说明

1.Python作为一门通用型脚本语言, 能像Java,C++那样处理通用型任务,比如读写二进制或文本文件. 读写文本文件很容易, 使用File Object即可操作, 读取字符串一般使用file.readline 或者 file.readlines, 一个是返回字符串对象, 一个是返回一个列表.

2.Python使用字符串对象存储二进制数据. 当调用file.read(n) 时, 返回一个字符串对象, 这个字符串对象类似于C++的 std::string 或者 char buf[], 都可以存储任何字节数据; 当需要对string对象,即字节数据进行操作时, 就需要unpack来进行数据转换, 比如把4个字节转换为数值, 把某部分的字节转换为str()字符串等等. Python的file对象类似于 C语言的FILE对象, 差不多有类似的对应函数.

3.对于struct.pack和unpack的说明, 其中 pack其实是对C结构体进行打包, 并进行本机默认地址对齐, 本地字节序. 比如 “bci” 的大小是8. 所以尽量使用i, 而不是b,c这些单字节, 因为被对齐后大小很难计算.

struct.pack(fmt, v1, v2, ...) 
Return a string containing the values v1, v2, ... packed according to the given format. The arguments must match the values required by the format exactly

struct.unpack(fmt, string) 
Unpack the string (presumably packed by pack(fmt, ...)) according to the given format. The result is a tuple even if it contains exactly one item. The string must contain exactly the amount of data required by the format (len(string) must equal calcsize(fmt)).

4.Byte Order, Size, and Alignment

Character Byte order Size Alignment
@ native native native
= native standard none
< little-endian standard none
> big-endian standard none
! network (= big-endian) standard

5.Format Characters

Format C Type Python type Standard size Notes
x pad byte no value
c char string of length 1 1
b signed char integer 1 (3)
B unsigned char integer 1 (3)
? _Bool bool 1 (1)
h short integer 2 (3)
H unsigned short integer 2 (3)
i int integer 4 (3)
I unsigned int integer 4 (3)
l long integer 4 (3)
L unsigned long integer 4 (3)
q long long integer 8 (2), (3)
Q unsigned long long integer 8 (2), (3)
f float float 4 (4)
d double float 8 (4)
s char[] string
p char[] string
P void * integer (5), (3)

例子

#! encoding=utf8

import sys
import os
import io
from StringIO import  StringIO
from struct import unpack
from struct import pack

def TestWriter(path):
    f = open(path,"wb")

    # write png header
    header = pack('BBBB',0x89,0x50,0x4E,0x47)
    f.write(header)

    one_str = "string中文"
    one_char1 = 0
    one_char2 = ord('t')
    one_int = 50
    # we need calc string length
    str_len = len(one_str)
    format = '%dsiii' % (str_len)
    body = pack(format,one_str,one_char1,one_char2,one_int)
    print len(body)

    f.write(body)
    f.close()

def TestReader(path):
    f = open(path,"rb")
    header = f.read(1)
    a, = unpack("B",header)
    png = f.read(3)
    print png

    name=''
    n=f.read(1)
    while unpack('<b',n)[0]!=0:
        name=name+str(n)
        n=f.read(1)

    print name
    # three bytes. == 0
    f.seek(3,io.SEEK_CUR)

    left = f.read(8)
    a,b = unpack("ii",left)
    print "%d:%d" % (a,b)

    f.close()

if __name__ == '__main__':
    path = ""
    if len(sys.argv) > 1:
        path = sys.argv[1];
    else:
        path = "temp.png"

    TestWriter(path);
    TestReader(path);
    # os.remove(path)

输出

24
PNG
string中文
116:50

参考

struct – Interpret strings as packed binary data
Python使用struct处理二进制
File Objects

猜你喜欢

转载自blog.csdn.net/infoworld/article/details/81033276