python读写二进制文件的方法

   本文实例讲述了python读写二进制文件的方法。分享给大家供大家参考。具体如下:

  初学python,现在要读一个二进制文件,查找doc只发现 file提供了一个read和write函数,而且读写的都是字符串,如果只是读写char等一个字节的还行,要想读写如int,double等多字节数 据就不方便了。在网上查到一篇贴子,使用struct模块里面的pack和unpack函数进行读写。下面就自己写代码验证一下。

  ?

1
2
3
4

>>> from struct import *
>>> file = open(r"c:/debug.txt", "wb")
>>> file.write(pack("idh", 12345, 67.89, 15))
>>> file.close()

  接着再将其读进来

  ?

1
2
3
4
5
6
7

>>> 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:

  ?

1
2
3
4

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

  方法2

  ?

1
2
3
4

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("x5Fx9Dx3E") 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 ="x45xF3" 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程序设计有所帮助。

时间: 2024-08-04 00:23:10

python读写二进制文件的方法的相关文章

python读写ini配置文件方法实例分析

  本文实例讲述了python读写ini配置文件方法.分享给大家供大家参考.具体实现方法如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 import ConfigParser import os class ReadWriteConfFile: currentDir=os.path.dirname(__file__) fil

Python读写配置文件的方法

  本文实例讲述了Python读写配置文件的方法.分享给大家供大家参考.具体分析如下: python 读写配置文件在实际应用中具有十分强大的功能,在实际的操作中也有相当简捷的操作方案,以下的文章就是对python 读写配置文件的具体方案的介绍,相信对大家学习Python有所帮助. python 读写配置文件ConfigParser模块是python自带的读取配置文件的模块.通过他可以方便的读取配置文件. 这里就来简单介绍一下python 读写配置文件的方法. 配置文件.顾名思议就是存放配置信息的

jscript读写二进制文件的方法

  jscript读写二进制文件的方法          这篇文章主要介绍了jscript读写二进制文件的方法,涉及javascript中ActiveXObject对象的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下 本文实例讲述了jscript读写二进制文件的方法.分享给大家供大家参考.具体实现方法如下: var bin = new Array(256); for(var i=0;i<256;i++){ bin[i]=String.fromCharCode(i); } function

jscript读写二进制文件的方法_javascript技巧

本文实例讲述了jscript读写二进制文件的方法.分享给大家供大家参考.具体实现方法如下: var bin = new Array(256); for(var i=0;i<256;i++){ bin[i]=String.fromCharCode(i); } function TestWrite(){ var Stream = new ActiveXObject("ADODB.Stream"); var adTypeBinary=1,adTypeText=2; Stream.Typ

用python读写excel的方法_python

本文实例讲述了用python读写excel的方法.分享给大家供大家参考.具体如下: 最近需要从多个excel表里面用各种方式整理一些数据,虽然说原来用过java做这类事情,但是由于最近在学python,所以当然就决定用python尝试一下了.发现python果然简洁很多.这里简单记录一下.(由于是用到什么学什么,所以不算太深入,高手勿喷,欢迎指导) 一.读excel表 读excel要用到xlrd模块,官网安装(http://pypi.python.org/pypi/xlrd).然后就可以跟着里面

Python读写Excel文件方法介绍_python

一.读取excel 这里介绍一个不错的包xlrs,可以工作在任何平台.这也就意味着你可以在Linux下读取Excel文件. 首先,打开workbook: 复制代码 代码如下: import xlrd wb = xlrd.open_workbook('myworkbook.xls') 检查表单名字: 复制代码 代码如下: wb.sheet_names() 得到第一张表单,两种方式:索引和名字    复制代码 代码如下: sh = wb.sheet_by_index(0) sh = wb.sheet

使用Python进行二进制文件读写的简单方法(推荐)_python

总的感觉,python本身并没有对二进制进行支持,不过提供了一个模块来弥补,就是struct模块. python没有二进制类型,但可以存储二进制类型的数据,就是用string字符串类型来存储二进制数据,这也没关系,因为string是以1个字节为单位的. import struct a=12.34 #将a变为二进制 bytes=struct.pack('i',a) 此时bytes就是一个string字符串,字符串按字节同a的二进制存储内容相同. 再进行反操作 现有二进制数据bytes,(其实就是字

Python读写文件方法总结

  本文实例总结了Python读写文件方法.分享给大家供大家参考.具体分析如下: 1.open 使用open打开文件后一定要记得调用文件对象的close()方法.比如可以用try/finally语句来确保最后能关闭文件. ? 1 2 3 4 5 file_object = open('thefile.txt') try: all_the_text = file_object.read( ) finally: file_object.close( ) 注:不能把open语句放在try块里,因为当打

python 读写、创建 文件的方法(必看)_python

python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd() 返回指定目录下的所有文件和目录名:os.listdir() 函数用来删除一个文件:os.remove() 删除多个目录:os.removedirs(r"c:\python") 检验给出的路径是否是一个文件:os.path.isfile() 检验给出的路径是否是一个目录:os.path.isdir() 判断是否是绝对路