read a files particular line
Peter L Hansen
peter at engcorp.com
Thu Oct 14 08:22:24 EDT 2004
Kristóf Stróbl wrote (top-posting, but I fixed it. You're welcome.):
> Frank Grellert wrote:
>> does anybody have an idea how to read a particular line of a file
>> like awk's awk '{if( NR==12) print $0}' filename (reads lineno 12 of
>> filename)
>
> If the file is not too big use:
>
> file("filename").readlines()[11]
And if the file is "too big", there's always iterators:
print itertools.islice(open('tempx.txt'), 11, 12).next()
A little more complicated, but avoids reading the entire file
into memory.
-Peter
More information about the Python-list
mailing list