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 相关文章推荐
python list 合并连接字符串的方法
Mar 09 Python
python正则表达式抓取成语网站
Nov 20 Python
python动态加载变量示例分享
Feb 17 Python
python装饰器decorator介绍
Nov 21 Python
Python使用BeautifulSoup库解析HTML基本使用教程
Mar 31 Python
Python运算符重载详解及实例代码
Mar 07 Python
python导入csv文件出现SyntaxError问题分析
Dec 15 Python
对命令行模式与python交互模式介绍
May 12 Python
使用pandas的DataFrame的plot方法绘制图像的实例
May 24 Python
python交互模式下输入换行/输入多行命令的方法
Jul 02 Python
Flask框架重定向,错误显示,Responses响应及Sessions会话操作示例
Aug 01 Python
Python进程,多进程,获取进程id,给子进程传递参数操作示例
Oct 11 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 set_include_path函数设置 include_path 配置选项
2016/10/30 PHP
PHP水印类,支持添加图片、文字、填充颜色区域的实现
2017/02/04 PHP
PHP 实现手机端APP支付宝支付功能
2018/06/07 PHP
PHP扩展类型及安装方式解析
2020/04/27 PHP
javascript 关于# 和 void的区别分析
2009/10/26 Javascript
JS学习之一个简易的日历控件
2010/03/24 Javascript
jqGrid jQuery 表格插件测试代码
2011/08/23 Javascript
动态标签 悬停效果 延迟加载示例代码
2013/11/21 Javascript
transport.js和jquery冲突问题的解决方法
2015/02/10 Javascript
JSONP之我见
2015/03/24 Javascript
jQuery简单入门示例之用户校验demo示例
2016/07/09 Javascript
js实现的在线调色板功能完整实例
2016/12/21 Javascript
浅析javaScript中的浅拷贝和深拷贝
2017/02/15 Javascript
JS触摸事件、手势事件详解
2017/05/04 Javascript
jQuery plugin animsition使用小结
2017/09/14 jQuery
JavaScript模块模式实例详解
2017/10/25 Javascript
jQuery实现列表的增加和删除功能
2018/06/14 jQuery
vue读取本地的excel文件并显示在网页上方法示例
2019/05/29 Javascript
js canvas实现5张图片合成一张图片
2019/07/15 Javascript
python 实现插入排序算法
2012/06/05 Python
Python设置Socket代理及实现远程摄像头控制的例子
2015/11/13 Python
浅谈Python中的作用域规则和闭包
2018/03/20 Python
分享一下Python数据分析常用的8款工具
2018/04/29 Python
python斐波那契数列的计算方法
2018/09/27 Python
Python 实现子类获取父类的类成员方法
2019/01/11 Python
python滑块验证码的破解实现
2019/11/10 Python
tensorflow获取预训练模型某层参数并赋值到当前网络指定层方式
2020/01/24 Python
Python 用__new__方法实现单例的操作
2020/12/11 Python
化工工艺专业求职信
2013/09/22 职场文书
我的网上商城创业计划书
2013/12/26 职场文书
索桥的故事教学反思
2014/02/06 职场文书
综治宣传月活动总结
2014/04/28 职场文书
工商干部先进事迹
2014/05/14 职场文书
《认识钟表》教学反思
2016/02/16 职场文书
2016见义勇为事迹材料汇总
2016/03/01 职场文书
Spring Boot项目如何优雅实现Excel导入与导出功能
2022/06/10 Java/Android