first, second, etc line of text file
Paul Rubin
http
Wed Jul 25 19:50:44 EDT 2007
"Daniel Nogradi" <nogradi at gmail.com> writes:
> A very simple question: I currently use a cumbersome-looking way of
> getting the first, second, etc. line of a text file:
>
> for i, line in enumerate( open( textfile ) ):
> if i == 0:
> print 'First line is: ' + line
> elif i == 1:
> print 'Second line is: ' + line
> .......
> .......
from itertools import islice
first_five_lines = list(islice(open(textfile), 5))
print 'first line is', first_five_lines[0]
print 'second line is', first_five_lines[1]
...
More information about the Python-list
mailing list