problem with logic in reading a binary file

Diez B. Roggisch deets at nospam.web.de
Sun Mar 30 08:23:39 EDT 2008


hdante schrieb:
> On Mar 30, 4:31 am, John Machin <sjmac... at lexicon.net> wrote:
>> On Mar 30, 3:58 pm, hdante <hda... at gmail.com> wrote:
>>
>>
>>
>>> On Mar 29, 3:44 pm, "Bryan.Fodn... at gmail.com"
>>> <Bryan.Fodn... at gmail.com> wrote:
>>>> Hello,
>>>> I am having trouble writing the code to read a binary string.  I would
>>>> like to extract the values for use in a calculation.
>>>> Any help would be great.
>>>  I'm too lazy to debug your binary string, but I suggest that you
>>> completely throw away the binary file and restart with a database or
>>> structured text. See, for example:
>>>  http://pyyaml.org/wiki/PyYAML
>>>  If you have some legacy binary file that you need to process, try
>>> creating a C program that freads the binary file and printfs a text
>>> equivalent.
>> ... and that couldn't be done faster and better in Python??
> 
>  No. A C struct is done faster and better than python (thus, the
> correctness check is faster in C). Also, chances are high that there's
> already an include file with the binary structure.

That is utter nonsense. There is no "correctness check" in C. and using 
printf & thus creating strings that you then need to parse in python 
just doubles the effort needlessly.

The standard-lib module "struct" is exactly what you need, nothing else. 
it sure is faster than any parsing of preprocessed data, doesn't 
introduce a language-mixture and is prototyped/tested much faster 
because of it being python - and not C-compiler and C-debugger.

Alternatively, *IF* there were C-structure-declarations available for 
the binary format, the usage of ctypes would allow for roughly the same, 
even reducing the effort to create the structure definition a great deal.

Diez



More information about the Python-list mailing list