我有一个显示在下面的文本文件。我想在Class 10块下选择2行(第1行和第2行)。
Thu May 29 14:16:00 PDT 2014
Class 09
line 0
line 1
line 2
--
Class 10
line 0
line 1
line 2
--
Class 11
line 0
line 1
line 2
--
Thu May 29 14:20:00 PDT 2014
Class 09
line 0
line 1
line 2
--
Class 10
line 0
line 1
line 2
--
Class 11
line 0
line 1
line 2
--
我尝试过以下操作,但linecache
仅获取第1行。我想找到一种先获取line1然后获取line2的方法。任何的想法?
感谢
numOflines = sum(1 for line in open('text.txt'))
print(num_lines)
for i in range(start,numOflines,step):
linea = linecache.getline('text.txt', i)
print linea
答案 0 :(得分:1)
numOflines = sum(1 for line in open('text.txt'))
这一行计算文件中的行数,就是这样。可能不是你想要的。
不确定为什么要linecache
。并且您没有显示计算start
或step
的方式,这可能是您的错误所在。
你想要的东西可能会是这样的:
def reading_function():
searching = True
linesToRead = 2
with open('text.txt') as f:
for line in f:
if searching and line.strip() == "Class 10":
searching = False
elif not searching:
print line.strip()
linesToRead -= 1
if linesToRead == 0:
return
答案 1 :(得分:1)
只是在玩耍。
我认为你不应该需要旗帜和操纵计数器,只需要一些可爱的循环。
lines_after_text = 2
search_text = "Class 10"
desired_lines = [] # in case search_text appears in text.txt more than once.
with open('text.txt') as f:
for line in f:
if search_text in line:
desired_lines.extend([next(f) for i in range(lines_after_text)])
答案 2 :(得分:0)
别忘了正则表达式!
with open(fn) as f:
for m in re.finditer(r'^(Class 10.*?)(?:^Class 11|\Z)', f.read(), re.M | re.S):
print m.group(1).splitlines()[2:4]
打印:
['line 1', 'line 2']
['line 1', 'line 2']
如果文件大于内存中的文件,请使用内存映射:
import re
import mmap
with open(fn) as f:
mm=mmap.mmap(f.fileno(),0,access=mmap.ACCESS_READ)
for m in re.finditer(r'^(Class 10.*?)(?:^Class 11|\Z)', mm, re.M | re.S):
print m.group(1).splitlines()[2:4]