i have binary file contains 4-byte binary values represent set of 2 short int
each. know can unpack single 4-byte binary value 2 short integers this:
from struct import unpack fval = b'\xba\x1e\x99\x01' #actualy read file qualdip, azi = unpack('hh', fval) print(type(qualdip), qualdip) print(type(azi), azi) >>> <class 'int'> 7866 >>> <class 'int'> 409
now, want unpack entire buffer. moment doing:
qualdips = [] azis = [] open(bfile, 'rb') buf: fval = buf.read(4) while fval: qualdip, azi = unpack('hh', fval) azis.append(azi) qualdips.append(qualdip) fval = buf.read(4)
which takes on minute 277mb file , seems produce huge memory overhead.
i unpack entire filebuffer directly 2 variables. how accomplish this?
i suspect struct.unpack_from
friend, unsure how formulate format.
with open(bfile, 'rb') buf: qualdip, azi = unpack_from('hh', buf)
only extracts 2 values, , (i know number of elements of file)
with open(bfile, 'rb') buf: qualdip, azi = unpack_from('72457091h72457091h', buf)
expects ridiculous amount of output variables. so:
how do unpack entire filebuffer directly 2 variables?
i don't know way unpack values directly 2 lists, can unpack entire file tuple , slice in two:
fval = b'\xba\x1e\x99\x01' * 3 unpacked= unpack('3h3h', fval) qualdip = unpacked[0::2] azi = unpacked[1::2]
alternatively, use islice
create iterator, reduce memory consumption.
qualdip = islice(unpacked, 0, none, 2) azi = islice(unpacked, 1, none, 2)
Comments
Post a Comment