read and write binary data (newbie)
Marcus Stojek
stojek at part-gmbh.de
Tue Feb 12 10:45:45 EST 2002
Hi,
I am using Python quite a while for all kind of things. Now i have to
read a file (Windows) in binary format that contains strings (\0
terminated), int and floats (4 byte). and I am stunned because
I don't know how to.
to read strings I have written a function:
-----------------------------
def getstring(dat,maxlen=8):
dummy=""
spos=0
while spos <= maxlen:
c=dat.read(1)
spos += 1
if c!="\0" :
dummy += c
else:
return dummy
return -1
----------------------------------
reading ints and floats is easy:
x= dat.read(4)
but how can i convert x into a number?
and how can I write a float or int into
a binary file?
Could anybody please tell me where to look?
thanks
marcus
More information about the Python-list
mailing list