python中使用struct模块处理二进制数据

本文介绍如何使用Python的struct模块正确读取并解析二进制文件中的整数类型数据,解决直接使用int转换导致的问题,并提供了一个简单的示例。

假设这样个场景:

你有个文件,里面全是二进制方式存储的整型,你需要读取出来。于是你随手写出了这样的代码:

1 = open("file","rb")
2 #读取个整型
3 data = f.read(4)
4 #读取完毕,关了文件
5 f.close()
6 #转换
7 num = int(data)

 然后就会报错:

 

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: null byte in argument for int()

 

你查看下data:

data = '8\x00:\x00'

 神马!!竟然是十六进制!!这货怎么转换?

你翻开《CookBook》 里面扯了一堆不着边用不到的东西,就是没有这个问题。绝望之时,你在文档里看到了struct模块,文档里这么说:

This module performs conversions between Python values and C structs represented as Python strings. It uses format strings (explained below) as compact descriptions of the lay-out of the C structs and the intended conversion to/from Python values. This can be used in handling binary data stored in files or from network connections, among other sources.

 没错,找的就是这货!

大概看了下,主要用到的是pack 函数和unpack函数。

pack:

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.

 unpack:

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)).

 

下面还有个对应C语言中数据类型的表格:

FormatC TypePythonNotes
xpad byteno value
ccharstring of length 1
bsigned charinteger
Bunsigned charinteger
?_Boolbool(1)
hshortinteger
Hunsigned shortinteger
iintinteger
Iunsigned intinteger or long
llonginteger
Lunsigned longlong
qlong longlong(2)
Qunsigned long longlong(2)
ffloatfloat
ddoublefloat
schar[]string
pchar[]string
Pvoid *long
 怎么样?大概看明白了吧?

你把代码改成了:

= open("file","rb")
data 
= f.read(4)
#转换格式
num = struct.unpack("i",data)
f.close()
print(num)

 

 这样就正常输出了吧?

嘿嘿,可以早点下班了。

 如果还不明白,就接着看文档去,还有可以参考下这篇文章:

http://www.cnblogs.com/tonychopper/archive/2010/07/23/1783501.html

 再次赞颂简洁万能的Python。

转载于:https://www.cnblogs.com/shiweifu/archive/2011/07/31/2122821.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值