How do I extract a ieee-be binary file embedded in a zipfile?
I have a set of zip files which contains several ieee-be encoded binary and text files. I have used Pythons ZipFile module and can extract the contents of the text file
def readPropFile(myZipFile):
zf = zipfile.ZipFile(myZipFile,'r') # Open zip file for reading
zFileList=zf.namelist() # extract list of files embedded in _myZipFile_
# text files in _myZipFile_ contain the word 'properties' so
# get a list of the property files here
for f in zFileList:
if f.find('properties')>0:
propFileList.append(f)
# open first file in propFileList
pp2 = cStringIO.StringIO(zf.read(propFileList[0]))
fileLines = []
for ll in pp2:
fileLines.append(ll)
# return the lines in the property text file
return fileLines
Now I would like to do the same sort of thing except reading the data in the binary files and creating an array of floats. So how would I proceed?
Update 1
The format of the binary files is such that in MATLAB I after extracting them to a temporary location I can read them with the following
>>fid=fopen('dataFile.bin'开发者_运维问答,'r','ieee-be');
>>dat=fread(fid,[1 inf],'float');
Update 2
I now have a simple function in attempt to read the binary data something like
def readBinaryFile(myZipFile):
zFile = zipfile.ZipFile(myZipFile,'r')
dataFileName = 'dataFile.bin'
stringData = zFile.read(dataFileName)
ss=stringData[0:4]
data=struct.unpack('>f',ss)
but the value I get does the same as the value reported in MATLAB.
Update 3
first float in my binary file
- HEX value: BD 98 99 3D
- float : -.07451103
Most of what you need is in this answer How do I convert a Python float to a hexadecimal string in python 2.5? Nonworking solution attached
See the stuff about struct.pack
.
More details on struct are in the Python docs
You could also try the Numpy extension (here), which is a bit lighter than SciPy. Numpy has lots of I/O routines. For example,
import numpy
f = file ('example.dat')
data_type = numpy.dtype ('float32').newbyteorder ('>')
x = numpy.fromfile (f, dtype=data_type)
gives you a numpy array. There's probably a less-clunky way to specify the data type.
In the snippet from the question, the "properties files" (whatever that is) are detected, in a rather loose fashion, by the presence of the string 'properties' in their contents, when read as text. I don't know what the equivalent of this would be for binary IEEE-le files.
However, with Python, a easy way to read ieee-le (or other formats) files is with SciPy's io.fopen module.
Edit
Since anyway reading such a binary file requires one to know the structure, you can express this in a struct.pack() format as desribed in Michael Dillon's response! This only requires the std library, and is just as easy!
精彩评论