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脚本对Linux服务器进行监控的教程
Apr 02 Python
Python实现SVN的目录周期性备份实例
Jul 17 Python
Python使用Srapy框架爬虫模拟登陆并抓取知乎内容
Jul 02 Python
[原创]教女朋友学Python(一)运行环境搭建
Nov 29 Python
python生成圆形图片的方法
Mar 25 Python
python中pika模块问题的深入探究
Oct 13 Python
pygame游戏之旅 载入小车图片、更新窗口
Nov 20 Python
关于Pytorch的MLP模块实现方式
Jan 07 Python
Python-openCV读RGB通道图实例
Jan 17 Python
Tensorflow: 从checkpoint文件中读取tensor方式
Feb 10 Python
Django celery异步任务实现代码示例
Nov 26 Python
python实战之一步一步教你绘制小猪佩奇
Apr 22 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.NET的入门教程
2006/10/09 PHP
编写漂亮的代码 - 将后台程序与前端程序分开
2008/04/23 PHP
PHP发明人谈MVC和网站设计架构 貌似他不支持php用mvc
2011/06/04 PHP
PHP容易忘记的知识点分享
2013/04/30 PHP
Drupal简体中文语言包安装教程
2014/09/27 PHP
Zend Framework框架Smarty扩展实现方法
2016/03/22 PHP
对textarea框的代码调试,而且功能上使用非常方便,酷
2006/06/30 Javascript
JS加ASP二级域名转向的代码
2007/05/17 Javascript
为javascript添加String.Format方法
2020/08/11 Javascript
这些年、我收集的JQuery代码小结
2012/08/01 Javascript
中国地区三级联动下拉菜单效果分析
2012/11/15 Javascript
js和php如何获取当前url的内容
2013/09/22 Javascript
js简单的弹出框有关闭按钮
2014/05/05 Javascript
JS动态改变表格边框宽度的方法
2015/03/31 Javascript
javascript消除window.close()的提示窗口
2015/05/20 Javascript
JavaScript中的small()方法使用详解
2015/06/08 Javascript
BootStrapTable 单选及取值的实现方法
2017/01/10 Javascript
详解vuejs之v-for列表渲染
2017/06/22 Javascript
Avalonjs双向数据绑定与监听的实例代码
2017/06/23 Javascript
AngularJS实现表单元素值绑定操作示例
2017/10/11 Javascript
JavaScript中利用Array filter() 方法压缩稀疏数组
2018/02/24 Javascript
angular6的table组件开发的实现示例
2018/12/26 Javascript
Angular(5.2->6.1)升级小结
2018/12/27 Javascript
jQuery实现的别踩白块小游戏完整示例
2019/01/07 jQuery
vue 中的 render 函数作用详解
2020/02/28 Javascript
[01:10:48]完美世界DOTA2联赛PWL S2 GXR vs PXG 第一场 11.18
2020/11/18 DOTA
python实现搜索指定目录下文件及文件内搜索指定关键词的方法
2015/06/28 Python
Python虚拟环境项目实例
2017/11/20 Python
对python 各种删除文件失败的处理方式分享
2018/04/24 Python
Django values()和value_list()的使用
2020/03/31 Python
Expedia韩国官网:亚洲发展最快的在线旅游门户网站
2018/02/26 全球购物
校长就职演讲稿
2014/01/06 职场文书
单位成立周年感言
2014/01/26 职场文书
优秀工会工作者事迹材料
2014/06/02 职场文书
不服劳动仲裁起诉书
2015/05/20 职场文书
python利用while求100内的整数和方式
2021/11/07 Python