reading specific lines of a file

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Sat Jul 15 19:15:23 EDT 2006


Pierre Quentel:
> If the line number of the first line is 0 :
> source=open('afile.txt')
> for i,line in enumerate(source):
>     if i == line_num:
>         break
> print line

I don't know if something like this can be called an improvement:

from itertools import islice
afile = file('data.txt')
line_n = 10
print islice(afile, line_n, line_n+1).next()

Bye,
bearophile




More information about the Python-list mailing list