[英]Reading specific lines from a file in a loop (optimization)
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. 我在“循环”中使用此代码花费了很长时间 ,其中“ line_number”每转都是随机的。 I also tried linecache.getline() but it needs to be call linecache.clearcache() every turn, so its not better.
我也尝试过linecache.getline(),但是每回合都需要调用linecache.clearcache(),所以它并不更好。
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. 我们可以使用
itertools.dropwhile()
忽略这些行,直到获得所需的行。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.