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.
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.
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]
def getLine(fileObj, lineNo):
[fileObj.readline() for i in xrange(lineNo - 1)]
return fileObj.readline()
Other way of doing
import linecache
linecache.getline('myfile', line_number)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.