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开发WebService系列教程之REST,web.py,eurasia,Django
Jun 30 Python
Python实现的一个找零钱的小程序代码分享
Aug 25 Python
python获取Linux下文件版本信息、公司名和产品名的方法
Oct 05 Python
用Python的线程来解决生产者消费问题的示例
Apr 02 Python
Python中动态检测编码chardet的使用教程
Jul 06 Python
python实现微信自动回复功能
Apr 11 Python
python绘制双Y轴折线图以及单Y轴双变量柱状图的实例
Jul 08 Python
python 实现读取csv数据,分类求和 再写进 csv
May 18 Python
Python实现进度条和时间预估的示例代码
Jun 02 Python
Python 防止死锁的方法
Jul 29 Python
python如何运行js语句
Sep 09 Python
Pytorch中TensorBoard及torchsummary的使用详解
May 12 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中的正则表达式函数介绍
2012/02/27 PHP
php加水印的代码(支持半透明透明打水印,支持png透明背景)
2013/01/17 PHP
浅析php学习的路线图
2013/07/10 PHP
PHP 多进程与信号中断实现多任务常驻内存管理实例方法
2019/10/04 PHP
利用PHP内置SERVER开启web服务(本地开发使用)
2020/01/22 PHP
PHP sdk文档处理常用代码示例解析
2020/12/09 PHP
javascript强大的日期函数代码分享
2013/09/04 Javascript
js抽奖实现随机抽奖代码效果
2013/12/02 Javascript
自己使用js/jquery写的一个定制对话框控件
2014/05/02 Javascript
jQuery.lazyload+masonry改良图片瀑布流代码
2014/06/20 Javascript
JS中prototype的用法实例分析
2015/03/19 Javascript
详解WordPress开发中get_current_screen()函数的使用
2016/01/11 Javascript
基于JavaScript实现随机颜色输入框
2016/12/10 Javascript
防止重复发送 Ajax 请求
2017/02/15 Javascript
JS仿QQ好友列表展开、收缩功能(第二篇)
2017/07/07 Javascript
node.js基于fs模块对系统文件及目录进行读写操作的方法详解
2017/11/10 Javascript
vue多页面开发和打包正确处理方法
2018/04/20 Javascript
vue组件从开发到发布的实现步骤
2018/11/11 Javascript
JS实现长图上下滚动效果
2020/03/19 Javascript
js实现tab栏切换效果
2020/08/02 Javascript
[02:12]2019完美世界全国高校联赛(春季赛)报名开启
2019/03/01 DOTA
Python 爬虫图片简单实现
2017/06/01 Python
Django生成PDF文档显示在网页上以及解决PDF中文显示乱码的问题
2019/07/04 Python
完美解决Pycharm中matplotlib画图中文乱码问题
2021/01/11 Python
html5小技巧之通过document.head获取head元素
2014/06/04 HTML / CSS
HTML5自定义属性的问题分析
2019/08/16 HTML / CSS
大学生优秀自荐信范文
2014/02/25 职场文书
社区网格化管理实施方案
2014/03/21 职场文书
我的理想演讲稿
2014/04/30 职场文书
五四青年节演讲稿
2014/05/26 职场文书
中学生社会实践活动总结
2014/07/03 职场文书
安全生产年活动总结
2014/08/29 职场文书
老兵退伍标语
2014/10/07 职场文书
专职安全员岗位职责
2015/04/11 职场文书
2015年材料员工作总结
2015/04/30 职场文书
php微信小程序解包过程实例详解
2021/03/31 PHP