使用 struct 模块需要使用 pack() 方法吧对象按指定个数进行序列化,然后使用文件对象的write方法将序列化的结果写入二进制文件;读取时需要使用文件对象的read()方法读取二进制文件内容,然后再使用struct模块的unpack()方法反序列化得到原来的信息。

  使用 struct 模块写入二进制文件。

 #导入模块
import struct #准备需要序列化的数据
n = 1300000000
x = 96.45
b = True
s = 'al@中国'
sn = struct.pack('if?',n,x,b) #序列化,i表示整数,f表示实数,?表示逻辑值
f = open('sample_struct.dat','wb')
f.write(sn) #将序列化后的对象写入文件
f.write(s.encode()) #字符串需要编码为字节串再写入文件
f.close() #使用struct模块读取上例中写入的二进制文件内容。
f = open('sample_struct.dat','rb')
sn = f.read(9)
tu = struct.unpack('if?',sn)
print(tu) #(1300000000, 96.44999694824219, True) 浮点数,python中保存的是不准确的 s = f.read(9)
s = s.decode() #字符串解码
print(s) #al@中国

  小提示:在上面的代码中,可能读者会疑惑如何确定要读取几个字节,为什么是 9 而不是其他数字呢?看完下面的代码应该能明白了。

 >>> import struct
>>> struct.pack('if?',13000,56.0,True)
b'\xc82\x00\x00\x00\x00`B\x01'
>>> len(_)
9
>>> struct.pack('if?',9999,5336.0,False)
b"\x0f'\x00\x00\x00\xc0\xa6E\x00"
>>> len(_)
9
>>>
>>> x = 'al@中国'
>>> len(x.encode())
9
>>>

6.3.2 使用struct模块读写二进制文件的更多相关文章

  1. 6.3.1 使用 pickle 模块读写二进制文件

    Python 标准库 pickle 提供的 dump() 方法 用于将数据进行序列化并写入文件(dump() 方法的protocol 参数为True 时可以实现压缩的效果),而load() 用于读取二 ...

  2. Python_struct模块操作二进制文件

    ''' 使用struct模块写入二进制文件 ''' import struct n=130000000 x=96.45 b=True s='a1@中国' sn=struct.pack('if?',n, ...

  3. python中struct模块及packet和unpacket

    转自:http://www.cnblogs.com/gala/archive/2011/09/22/2184801.html 我们知道python只定义了6种数据类型,字符串,整数,浮点数,列表,元组 ...

  4. day30 python学习 struct模块和 subprocess 模块

    import subprocess import struct aa=input('>>') obj=subprocess.Popen(aa,shell=True,#aa代表的是读取字符串 ...

  5. 字节流与数据类型的相互转换---使用struct模块

    字节流与数据类型的相互转换---使用struct模块 http://blog.csdn.net/Sunboy_2050/article/details/5974029 Python是一门非常简洁的语言 ...

  6. python使用xlrd模块读写Excel文件的方法

    本文实例讲述了python使用xlrd模块读写Excel文件的方法.分享给大家供大家参考.具体如下: 一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi ...

  7. Python学习——struct模块的pack、unpack示例

    he struct module includes functions for converting between strings of bytes and native Python data t ...

  8. c语言write与python的struct模块交互

    以下讲的都是用二进制形式打开文件.网上有很多struct模块的文章,下面是我做的小实验. 1.对于c里面的fwrite写入一个单字节,写的就是它的二进制.如3,写入文件就是二进制0x03,它并不是3的 ...

  9. 浅析Python中的struct模块

    最近在学习python网络编程这一块,在写简单的socket通信代码时,遇到了struct这个模块的使用,当时不太清楚这到底有和作用,后来查阅了相关资料大概了解了,在这里做一下简单的总结. 了解c语言 ...

随机推荐

  1. python pass 的用法

    python pass用法 1.空语句 do nothing 2.保证格式完整 3.保证语义完整 4.以if语句为例: C/C++中写法: if(true) ; // do nothing else ...

  2. poj2262 Goldbach's Conjecture——筛素数

    题目:http://poj.org/problem?id=2262 水水更健康~ 代码如下: #include<iostream> #include<cstdio> #incl ...

  3. bzoj1008 [HNOI2008]越狱——快速幂

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1008 (这样一道水题还因为忘记写 %lld WA了那么多遍) 发生越狱的状态数,就是全部状态 ...

  4. openstack instance bootmgr is missing 问题 修复

  5. ubuntu下如何查看和设置分辨率 (转载)

    转自:http://blog.csdn.net/jcgu/article/details/8650423 在ubuntu下可以使用xrandr来设置自己需要的分辨率.大致步骤如下: 1.使用xrand ...

  6. AcWing算法基础1.2

    排序 归并排序 归并排序和快速排序相反,快排是先排后分再合并,归并则是先分后排再合并 归并排序时间复杂度是O(n logn) 分析:    ------------------------------ ...

  7. OpenResty / Nginx模块,Lua库和相关资源的列表

    OpenResty / Nginx模块,Lua库和相关资源的列表 什么是OpenResty OpenResty是一个成熟的网络平台,它集成了标准的Nginx核心,LuaJIT,许多精心编写的Lua库, ...

  8. python自动化测试学习笔记-6urllib模块&request模块

    python3的urllib 模块提供了获取页面的功能. urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capat ...

  9. 题解报告:hdu 1285 确定比赛名次

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 Problem Description 有N个比赛队(1<=N<=500),编号依次 ...

  10. 355 Design Twitter 设计推特

    设计一个简化版的推特(Twitter),可以让用户实现发送推文,关注/取消关注其他用户,能够看见关注人(包括自己)的最近十条推文.你的设计需要支持以下的几个功能:    postTweet(userI ...