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自然语言编码转换模块codecs介绍
Apr 08 Python
Python基于Tkinter实现的记事本实例
Jun 17 Python
使用Python编写简单的画图板程序的示例教程
Dec 08 Python
Python中%r和%s的详解及区别
Mar 16 Python
Django进阶之CSRF的解决
Aug 01 Python
PyTorch的深度学习入门之PyTorch安装和配置
Jun 27 Python
使用OpenCV实现仿射变换—缩放功能
Aug 29 Python
基于python3.7利用Motor来异步读写Mongodb提高效率(推荐)
Apr 29 Python
解决python 虚拟环境删除包无法加载的问题
Jul 13 Python
Python文件夹批处理操作代码实例
Jul 21 Python
pycharm如何使用anaconda中的各种包(操作步骤)
Jul 31 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
PHP中的替代语法介绍
2015/01/09 PHP
php操作(删除,提取,增加)zip文件方法详解
2015/03/12 PHP
PHP准确取得服务器IP地址的方法
2015/06/02 PHP
PHP使用glob方法遍历文件夹下所有文件的实例
2018/10/17 PHP
jquery 输入框数字限制插件
2009/11/10 Javascript
JavaScript replace(rgExp,fn)正则替换的用法
2010/03/04 Javascript
Raphael一个用于在网页中绘制矢量图形的Javascript库
2013/01/08 Javascript
Chrome扩展页面动态绑定JS事件提示错误
2014/02/11 Javascript
js验证IP及子网掩码的合法性有效性示例
2014/04/30 Javascript
js如何改变文章的字体大小
2016/01/08 Javascript
js改变透明度实现轮播图的算法
2020/08/24 Javascript
jQuery时间日期三级联动(推荐)
2016/11/27 Javascript
关于微信上网页图片点击全屏放大效果
2016/12/19 Javascript
判断横屏竖屏(三种)
2017/02/13 Javascript
Node.js应用设置安全的沙箱环境
2018/04/23 Javascript
chosen实现省市区三级联动
2018/08/16 Javascript
vue中eslintrc.js配置最详细介绍
2018/12/21 Javascript
vue-cli点击实现全屏功能
2020/03/07 Javascript
JS替换字符串中指定位置的字符(多种方法)
2020/05/28 Javascript
[05:03]2018DOTA2亚洲邀请赛主赛事首日回顾
2018/04/04 DOTA
[02:51]DOTA2 Supermajor小组分组对阵抽签仪式
2018/06/01 DOTA
Python标准模块--ContextManager上下文管理器的具体用法
2017/11/27 Python
对Python中数组的几种使用方法总结
2018/06/28 Python
python 检查是否为中文字符串的方法
2018/12/28 Python
python 求1-100之间的奇数或者偶数之和的实例
2019/06/11 Python
Python爬虫破解登陆哔哩哔哩的方法
2020/11/17 Python
python给list排序的简单方法
2020/12/10 Python
CSS3自定义滚动条样式的示例代码
2017/08/21 HTML / CSS
深入理解HTML的FormData对象
2016/05/17 HTML / CSS
兰蔻美国官网:Lancome美国
2017/04/25 全球购物
数据库方面面试题
2012/04/22 面试题
初中生自我鉴定
2014/02/04 职场文书
大家访活动实施方案
2014/03/10 职场文书
2014年合同管理工作总结
2014/12/02 职场文书
安全教育主题班会总结
2015/08/14 职场文书
在redisCluster中模糊获取key方式
2021/07/09 Redis