how to do reading of binary files?

Diez B. Roggisch deets at nospam.web.de
Fri Jun 8 08:30:15 EDT 2007


jvdb schrieb:
> On 8 jun, 14:07, "Diez B. Roggisch" <d... at nospam.web.de> wrote:
>> jvdb schrieb:
> ......
>> What has the searching to do with the reading? 10MB easily fit into the
>> main memory of a decent PC, so just do
>>
>> contents = open("file").read() # yes I know I should close the file...
>>
>> print contents.find('\x0c')
>>
>> Diez
> 
> True. But there is another issue attached to the one i wrote.
> When i know how much this occurs, i know the amount of pages in the
> file. After that i would like to be able to extract a given amount of
> data:
> file x contains 20 <0C>. then for example i would like to extract from
> instance 5 to instance 12 from the file.
> The reason why i want to do this: The 0C stands for a pagebreak in PCL
> language. This way i would be absle to extract a certain amount of
> pages from the file.

And? Finding the respective indices by using

last_needle_position = 0
positions = []
while last_needle_position != -1:
     last_needle_position = contents.find(needle, last_needle_position+1)
     if last_needle_position != -1:
         positions.append(last_needle_position)


will find all the pagepbreaks. then just slice contents appropriatly. 
Did you read the python tutorial?

diez



More information about the Python-list mailing list