ANN: binario - simple work with binary files
timr at probo.com
Sat Sep 6 20:34:12 CEST 2014
Rustom Mody <rustompmody at gmail.com> wrote:
>On Tuesday, September 2, 2014 6:05:19 AM UTC+5:30, Tim Roberts wrote:
>> Rustom Mody wrote:
>> >On Tuesday, August 26, 2014 6:58:42 AM UTC+5:30, Tim Roberts wrote:
>> >> To the equivalent code with struct:
>> >> import struct
>> >> dscrp = "H?fs5B"
>> >> f = open('file.dat')
>> >> stuff = struct.unpack( dscrp, f.read() )
>> >> print stuff
>> >> In both cases, you have to KNOW the format of the data beforehand. If you
>> >> do a read_short where you happen to have written a float, disaster ensues.
>> >> I don't really see that you've added very much.
>> >I thought much the same.
>> >However notice your f.read(). Its type is string.
>> >What if file.dat is a 1GB wav file?
>> stuff = struct.unpack( dscrp, f.read(128) )
>And what if the struct you are (trying to) unpack is greater or less
>than 128 bytes?
stuff = struct.unpack( dscrp, f.read(struct.calcsize(dscrp)) )
>But its not enough. One can write a generator that yields one char
>at a time. How to feed that to struct.unpack??
I think you have lost track of the discussion here, because your question
is irrelevant. His binario package couldn't do that, either, since it only
accepts filenames. But at least with struct.unpack, I can suck from the
generator into a string, and feed that string into struct.unpack.
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.
More information about the Python-list