Python; jump to a concrete line

Chris cwitts at gmail.com
Thu Dec 20 14:02:07 EST 2007


On Dec 20, 8:13 pm, "Russell Blau" <russb... at hotmail.com> wrote:
> "Horacius ReX" <horacius.... at gmail.com> wrote in message
>
> news:5c5cbbd1-cc3c-4229-b6c1-753b2e538681 at p69g2000hsa.googlegroups.com...
>
> > Hi, sorry but after looking for information, I still did not get how,
> > when reading a text file in python, can one jump to a concrete line
> > and then read the different data (separated by spaces). In each line
> > there is different number of columns so sometimes i get kind of "index
> > out" error. Is there a better way to read the different data on each
> > row and avoiding to know the exact number of columns ?
>
> Have you considered using the file.readlines() method?
>
> Russ

.readlines() will hog some resources on large files.
file.read().splitlines()[ line_to_seek ]
might be the fastest way for small files, but also will swell for
larger ones.
Also, one line way to get what you want:
[a for (i,a) in enumerate(input_file) if i == line_to_seek]



More information about the Python-list mailing list