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