[Tutor] Reading every 5th line
Dave Angel
davea at ieee.org
Sun Aug 8 21:43:00 CEST 2010
nitin chandra wrote:
> Hello Everyone,
>
> I am to make a small programme for a friend of mine
> where i am to start reading from 14th (string) from a file and then
> read every 5th row.
>
> ie.
>
> in 1st read it reads the 14 row in a File, write to an OUTPUT-1 file
> Next reads 19th row, write to the OUTPUT-1 file
> then 24th row,... so on.
>
> and the second loop does is
>
> reads the 15th line as the first line from same input file and write
> to OUTPUT-2 file
> next reads 20th line / row, write to the OUTPUT-2 file
>
>
> I have tried various ways but some how i am ONLY able to do i simple
> read from one file and write to another.
>
> There are 3024 rows / lines PER file and there are 24 such file I need
> to run the programme on.
>
> I really need this on an urgent basis. can some one please help me in this.
>
> Thanks in Advance
>
> Nitin
>
>
Write a function that takes a pair of filenames and the starting line #
to begin reading. Then call it in a loop from 14 through 19. And call
that loop for each input file.
The loop inside the function body could be as simple as (untested):
for line in itertools.islice(infile, startnum, 10**6, 5):
outfile.write(line)
DaveA
More information about the Tutor
mailing list