seeking thru a file

Nils Rüttershoff nils at ccsg.de
Fri Jun 26 14:00:58 CEST 2009


Hi Mag,

Mag Gam wrote:
> I have a compressed CSV gziped file. I was wondering if it is possible
> to seek thru a file
>
> For example:
>
> I want to load the first 100 lines into an array. Process the data
>
> Seek from 101 line to 200 lines. Process the data (remove lines 0 -
> 100) from memory
>
> Seek 201 to 300 line. Process the data (remove 200-300)) from memory
>
> etc..etc..
>   

This would be very easy. Here is one way you could do it:
(I didn't test the code; I've just write it down, assuming you use
python 2.6, cause you didn't mentioned which version you are using...)

import gzip

step = 100
data_present = True
with gzip.open(foo.csv.gzip) as my_file:
    counter = 0
    my_buffer = []
    while data_present:
        while counter <= step:
            line = my_file.readline()
            if line:
                my_buffer.append(my_file.readline())
                counter += 1
            else:
                data_present = False
                break
        if len(my_buffer) > 0:
            do_something(my_buffer)
            counter = 0
            my_buffer = []

        
Kind Regards, Nils



More information about the Python-list mailing list