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共享引用(多个变量引用)示例代码
Dec 04 Python
python入门教程之识别验证码
Mar 04 Python
如何利用python查找电脑文件
Apr 27 Python
Python3获取拉勾网招聘信息的方法实例
Apr 03 Python
解决yum对python依赖版本问题
Jul 05 Python
Python 获取命令行参数内容及参数个数的实例
Dec 20 Python
如何基于python测量代码运行时间
Dec 25 Python
python函数定义和调用过程详解
Feb 09 Python
pycharm无法导入本地模块的解决方式
Feb 12 Python
对python中list的五种查找方法说明
Jul 13 Python
Python基础之tkinter图形化界面学习
Apr 29 Python
Python函数对象与闭包函数
Apr 13 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
在PHP3中实现SESSION的功能(二)
2006/10/09 PHP
Php Cookie的一个使用注意点
2008/11/08 PHP
php csv操作类代码
2009/12/14 PHP
WebQQ最新登陆协议的用法
2014/12/22 PHP
php列出mysql表所有行和列的方法
2015/03/13 PHP
PHP实现图片的等比缩放和Logo水印功能示例
2017/05/04 PHP
Laravel手动返回错误码示例
2019/10/22 PHP
fromCharCode和charCodeAt 方法
2006/12/27 Javascript
JQuery 国际象棋棋盘 实现代码
2009/06/26 Javascript
javascript 页面只自动刷新一次
2009/07/10 Javascript
JavaScript 常见对象类创建代码与优缺点分析
2009/12/07 Javascript
Firefox/Chrome/Safari的中可直接使用$/$$函数进行调试
2012/02/13 Javascript
js浮动图片的动态效果
2013/07/10 Javascript
Jquery使用val方法读写value值
2015/05/18 Javascript
js实现用户离开页面前提示是否离开此页面的方法(包括浏览器按钮事件)
2015/07/18 Javascript
纯JS实现轮播图
2017/02/22 Javascript
jQuery实现页码跳转式动态数据分页
2017/12/31 jQuery
vue过滤器用法实例分析
2019/03/15 Javascript
Vue使用.sync 实现父子组件的双向绑定数据问题
2019/04/04 Javascript
微信小程序如何访问公众号文章
2019/07/08 Javascript
node.js中Buffer缓冲器的原理与使用方法分析
2019/11/23 Javascript
python链接Oracle数据库的方法
2015/06/28 Python
python的pandas工具包,保存.csv文件时不要表头的实例
2018/06/14 Python
python定向爬虫校园论坛帖子信息
2018/07/23 Python
python实现名片管理系统项目
2019/04/26 Python
Python语法分析之字符串格式化
2019/06/13 Python
如何安装并在pycharm使用selenium的方法
2020/04/30 Python
python Gabor滤波器讲解
2020/10/26 Python
如何基于Python爬虫爬取美团酒店信息
2020/11/03 Python
美国婴儿用品店:Babies”R”Us
2017/10/12 全球购物
超市促销实习自我鉴定
2013/09/23 职场文书
职业规划书如何设计?
2014/01/09 职场文书
单位委托书格式范本
2014/09/29 职场文书
离婚协议书怎么写
2015/01/26 职场文书
《领导干部从政道德启示录》学习心得体会
2016/01/20 职场文书
python实现三次密码验证的示例
2021/04/29 Python