我有一个大约 100000 行的巨大文本文件,我想阅读它。我对所有内容都不感兴趣。我想搜索以“Residue XXX”开头的行并从那里阅读接下来的三行。我不想读取缓冲区列表中的整行。有没有一种有效的方法来搜索这条线并从那里开始阅读
f=open("result.txt",r)
lines = f.readlines()// NOT preferred
如果有办法在整个文件中搜索这一行而不是读取它们并进行迭代,我只需要一些输入。
您正在寻找类似的东西:
read_lines = None
for l in open("result.txt"):
if read_lines:
print l.encode('unicode_escape')
read_lines -= 1
if read_lines == 0:
break
if l.startswith('Residue ddG RotamerProbability'):
read_lines = 3
有更巧妙的解决方案,但这很简单。
除了读取数据之外,实际上没有任何方法可以在文件中进行搜索。有或多或少有效率的方法来读取数据,因此例如在 C 中执行它可能比在 Python 中的循环更快,但大概 Python 是您想要使用的。
该itertools
模块提供了两个与您想要的功能相关的功能:dropwhile
搜索具有特定属性的值并islice
从可迭代中选择一系列值:
import itertools
with open('result.txt') as infile:
def predicate(line):
return not line.startswith('Residue XXX')
result = list(itertools.islice(itertools.dropwhile(predicate, infile), 1, 4))
print result
with open("result.txt") as f:
# find line starting with Residue XXX
next(line for line in f if not line.startswith("Residue XXX"))
# get next three lines into a list (empty string for nonexistent lines)
results = [next(f, "").rstrip() for line in range(3)]
如果您想将该Residue XXX
行保留为列表的第一项results
:
with open("result.txt") as f:
# find line starting with Residue XXX
results = [next(line for line in f if not line.startswith("Residue XXX").rstrip()]
# add next three lines to the list (empty string for nonexistent lines)
results.extend(next(f, "").rstrip() for line in range(3))
文件对象是一个迭代器,如果您在不同的地方使用它,它将继续存在。islice 是一个方便的函数,可以从迭代器中获取项目。将它们放在一起,使用 for 循环查找起始位置并使用 islice 来获取其余部分。
我不确定您是否想在列表中包含匹配的行,或者您想对行尾做什么,所以我决定添加匹配的行加上接下来的 3 行,而不是尾随换行符。
from itertools import islice
with open('result.txt') as f:
for line in f:
if line.startswith("Residue XXX"):
my_list = [line.strip()]
my_list.extend(extra.strip() for extra in islice(f, 3))
break