python读写二进制文件的方法


Posted in Python onMay 09, 2015

本文实例讲述了python读写二进制文件的方法。分享给大家供大家参考。具体如下:

初学python,现在要读一个二进制文件,查找doc只发现 file提供了一个read和write函数,而且读写的都是字符串,如果只是读写char等一个字节的还行,要想读写如int,double等多字节数 据就不方便了。在网上查到一篇贴子,使用struct模块里面的pack和unpack函数进行读写。下面就自己写代码验证一下。

>>> from struct import *
>>> file = open(r"c:/debug.txt", "wb")
>>> file.write(pack("idh", 12345, 67.89, 15))
>>> file.close()

接着再将其读进来

>>> file = open(r"c:/debug.txt", "rb")
>>> (a,b,c) = unpack("idh",file.read(8+8+2))
>>> a,b,c
(12345, 67.890000000000001, 15)
>>> print a,b,c
12345 67.89 15
>>> file.close()

在操作过程中需要注意数据的size

注意  wb,rb中的b字,一定不可以少

方法1:

myfile=open('c:\\t','rb')
s=myfile.read(1)
byte=ord(s) #将一个字节 读成一个数
print hex(byte) #转换成16进制的字符串

方法2

import struct
myfile=open('c:\\t','rb').read(1)
print struct.unpack('c',myfile)
print struct.unpack('b',myfile)

写入

To open a file for binary writing is easy, it is the same way you do for reading, just change the mode into “wb”.
file = open("test.bin","wb")
But, how to write the binary byte into the file?
You may write it straight away with hex code like this:
file.write("\x5F\x9D\x3E") file.close()
Now, check it out with hexedit,
hexedit test.bin
You will see this:
00000000 5F 9D 3E _.> 00000020 00000040
Now, open the file to append more bytes:
file = open("test.bin","ab")
What if I want to store by bin value into a stream and write it one short?
s ="\x45\xF3" s = s + "%c%c" % (0x45,0xF3) file.write(s) file.close()
Any convenient ways if I can obtained a hex string, and want to convert it back to binary format?
Yes, you just need to import binascii
import binascii hs="5B7F888489FEDA" hb=binascii.a2b_hex(hs) file.write(hb) file.close()

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
Python3基础之条件与循环控制实例解析
Aug 13 Python
给Python IDLE加上自动补全和历史功能
Nov 30 Python
Python中使用dom模块生成XML文件示例
Apr 05 Python
python 输出上个月的月末日期实例
Apr 11 Python
Python中关键字global和nonlocal的区别详解
Sep 03 Python
python取数作为临时极大值(极小值)的方法
Oct 15 Python
在pycharm下设置自己的个性模版方法
Jul 15 Python
python中有关时间日期格式转换问题
Dec 25 Python
python打印文件的前几行或最后几行教程
Feb 13 Python
Python sklearn中的.fit与.predict的用法说明
Jun 28 Python
使用numpy nonzero 找出非0元素
May 14 Python
Python 居然可以在 Excel 中画画你知道吗
Feb 15 Python
Python求导数的方法
May 09 #Python
Python itertools模块详解
May 09 #Python
python读取word文档的方法
May 09 #Python
python动态性强类型用法实例
May 09 #Python
Python functools模块学习总结
May 09 #Python
Python浅拷贝与深拷贝用法实例
May 09 #Python
九步学会Python装饰器
May 09 #Python
You might like
写一段简单的PHP建立文件夹代码
2015/01/06 PHP
PHP随机生成唯一HASH值自定义函数
2015/04/20 PHP
日常整理PHP中简单的图形处理(经典)
2015/10/26 PHP
浅析Yii2缓存的使用
2016/05/10 PHP
Thinkphp5框架实现获取数据库数据到视图的方法
2019/08/14 PHP
JS解密入门 最终变量劫持
2008/06/25 Javascript
javascript小数计算出现近似值的解决办法
2010/02/06 Javascript
返回对象在当前级别中是第几个元素的实现代码
2011/01/20 Javascript
js异常捕获方法介绍
2013/04/10 Javascript
jquery实现弹出层完美居中效果
2014/03/03 Javascript
JS替换字符串中空格方法
2015/04/17 Javascript
JavaScript重载函数实例剖析
2016/05/13 Javascript
Vue.js每天必学之Class与样式绑定
2016/09/05 Javascript
Java  Spring 事务回滚详解
2016/10/17 Javascript
webpack学习教程之前端性能优化总结
2017/12/05 Javascript
JavaScript中使用import 和require打包后实现原理分析
2018/03/07 Javascript
vue项目中极验验证的使用代码示例
2019/12/03 Javascript
[51:07]VGJ.S vs Pain 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/20 DOTA
numpy 计算两个数组重复程度的方法
2018/11/07 Python
pyside+pyqt实现鼠标右键菜单功能
2020/12/08 Python
Django认证系统实现的web页面实现代码
2019/08/12 Python
pygame实现俄罗斯方块游戏(对战篇1)
2019/10/29 Python
python pandas利用fillna方法实现部分自动填充功能
2020/03/16 Python
Python如何合并多个字典或映射
2020/07/24 Python
来自Ocado的宠物商店:Fetch
2018/07/10 全球购物
G-Form护具官方网站:美国运动保护装备
2019/09/04 全球购物
学生自我鉴定
2013/12/18 职场文书
物业门卫岗位职责
2013/12/28 职场文书
家电业务员岗位职责
2014/03/10 职场文书
周年庆促销方案
2014/03/15 职场文书
美丽人生观后感
2015/06/03 职场文书
父亲去世追悼词
2015/06/23 职场文书
小学毕业教师寄语
2019/06/21 职场文书
python办公自动化之excel的操作
2021/05/23 Python
python如何查找列表中元素的位置
2022/05/30 Python
Python中np.random.randint()参数详解及用法实例
2022/09/23 Python