Reputation: 1017
I'm reading specific lines from a text file. Here is my code:
file.seek(0)
for j, ln in enumerate(file):
if j == line_number
line = ln # i'm getting the line
break
It tooks a long time when I use this code in a "loop" where the line_number is random every turn. I also tried linecache.getline() but it needs to be call linecache.clearcache() every turn, so its not better.
Is there a faster solution for that? Memory doesn't matter.
Upvotes: 1
Views: 1433
Reputation: 31568
Other way of doing
import linecache
linecache.getline('myfile', line_number)
Upvotes: 0
Reputation: 89097
If memory truly doesn't matter:
lines = file.readlines()
line = lines[line_number]
You read the file in once, store it in memory, and access it from there.
Edit: Where memory matters:
from itertools import dropwhile
line = next(dropwhile(lambda x: x[0]==line_number, enumerate(file)))[1]
We can use itertools.dropwhile()
to ignore the lines until we get to the one we need.
Upvotes: 3
Reputation: 24788
def getLine(fileObj, lineNo):
[fileObj.readline() for i in xrange(lineNo - 1)]
return fileObj.readline()
Upvotes: 1
Reputation: 251166
use this above the loop:
with open('in.txt') as f:
lines=[ln for ln in f]
now access any line number inside the loop:
lines[line_number]
Upvotes: 1