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的pandas框架操作Excel文件中的数据教程
Mar 31 Python
Python之pandas读写文件乱码的解决方法
Apr 20 Python
python list转矩阵的实例讲解
Aug 04 Python
Dlib+OpenCV深度学习人脸识别的方法示例
May 14 Python
如何更优雅地写python代码
Jul 02 Python
TensorFlow2.0:张量的合并与分割实例
Jan 19 Python
python匿名函数lambda原理及实例解析
Feb 07 Python
tensorflow 实现数据类型转换
Feb 17 Python
python实现微信打飞机游戏
Mar 24 Python
django实现HttpResponse返回json数据为中文
Mar 27 Python
python调用摄像头的示例代码
Sep 28 Python
python中os.remove()用法及注意事项
Jan 31 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 5.0对象模型深度探索之绑定
2006/09/05 PHP
PHP脚本的10个技巧(1)
2006/10/09 PHP
php中的时间显示
2007/01/18 PHP
AJAX PHP无刷新form表单提交的简单实现(推荐)
2016/09/09 PHP
Laravel多用户认证系统示例详解
2018/03/13 PHP
Javascript中的数学函数集合
2007/05/08 Javascript
JS 页面内容搜索,类似于 Ctrl+F功能的实现代码
2007/08/13 Javascript
javascript中call,apply,bind的用法对比分析
2015/02/12 Javascript
跟我学习javascript的prototype原型和原型链
2015/11/18 Javascript
使用Bootstrap + Vue.js实现添加删除数据示例
2017/02/27 Javascript
微信小程序左右滑动的实现代码
2017/12/15 Javascript
axios post提交formdata的实例
2018/03/16 Javascript
vue之将echart封装为组件
2018/06/02 Javascript
JavaScript实现封闭区域布尔运算的示例代码
2018/06/25 Javascript
基于Vue实现微信小程序的图文编辑器
2018/07/25 Javascript
详解js模板引擎art template数组渲染的方法
2018/10/09 Javascript
tsconfig.json配置详解
2019/05/17 Javascript
layui表格分页 记录勾选的实例
2019/09/02 Javascript
解决包含在label标签下的checkbox在ie8及以下版本点击事件无效果兼容的问题
2019/10/27 Javascript
Bootstrap实现前端登录页面带验证码功能完整示例
2020/03/26 Javascript
微信小程序开发(一):服务器获取数据列表渲染操作示例
2020/06/01 Javascript
Vue 实例中使用$refs的注意事项
2021/01/29 Vue.js
[44:50]DOTA2上海特级锦标赛B组小组赛#2 VG VS Fnatic第二局
2016/02/26 DOTA
Python面向对象类继承和组合实例分析
2018/05/28 Python
Django项目中model的数据处理以及页面交互方法
2018/05/30 Python
Python 装饰器原理、定义与用法详解
2019/12/07 Python
建筑专业毕业生推荐信
2013/11/21 职场文书
《陶罐和铁罐》教学反思
2014/02/19 职场文书
《圆明园的毁灭》教学反思
2014/02/28 职场文书
优秀乡村医生事迹材料
2014/05/28 职场文书
酒店辞职书范文
2015/02/26 职场文书
撤诉状格式范本
2015/05/19 职场文书
只用50行Python代码爬取网络美女高清图片
2021/06/02 Python
RPM包方式安装Oracle21c的方法详解
2021/08/23 Oracle
宝塔更新Python及Flask项目的部署
2022/04/11 Python
详解Mysql数据库平滑扩容解决高并发和大数据量问题
2022/05/25 MySQL