• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python读写二进制文件的方法

python读写二进制文件的方法

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python 二进制文件,python读取二进制文件,python 写二进制文件,python 读二进制文件,c#二进制文件读写等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了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()
</div>

接着再将其读进来

>>> 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()
</div>

在操作过程中需要注意数据的size

注意  wb,rb中的b字,一定不可以少

方法1:

myfile=open('c:\\t','rb')
s=myfile.read(1)
byte=ord(s) #将一个字节 读成一个数
print hex(byte) #转换成16进制的字符串
</div>

方法2

import struct
myfile=open('c:\\t','rb').read(1)
print struct.unpack('c',myfile)
print struct.unpack('b',myfile)
</div>

写入

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程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • 使用Python进行二进制文件读写的简单方法(推荐)
  • 使用Python内置的模块与函数进行不同进制的数的转换
  • 使用Python内置的模块与函数进行不同进制的数的转换
  • python处理二进制数据的方法
  • Python 26进制计算实现方法
  • Python 26进制计算实现方法
  • python读写二进制文件的方法

相关文章

  • Python中用startswith()函数判断字符串开头的教程
  • 用Python编写一个基于终端的实现翻译的脚本
  • 部署Python的框架下的web app的详细教程
  • Python实现简单状态框架的方法
  • python的几种开发工具介绍
  • Using Django with GAE Python 后台抓取多个网站的页面全文
  • Python中的数据对象持久化存储模块pickle的使用示例
  • Python随手笔记之标准类型内建函数
  • Python读取图片属性信息的实现方法
  • Python中dictionary items()系列函数的用法实例

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python字符串转换成浮点数函数分享
    • RC4文件加密的python实现方法
    • python脚本实现分析dns日志并对受访域名排行
    • Python中的多行注释文档编写风格汇总
    • 编写Python脚本来获取Google搜索结果的示例
    • python使用wxpython开发简单记事本的方法
    • Python 2.7.x 和 3.x 版本的重要区别小结
    • python实现查询IP地址所在地
    • python之yield表达式学习
    • Python线程的两种编程方式

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有