我有一个巨大的文件,如下所示:
Name: Block of line 1
Data: Block of line 2
**Important line 3**
Not important line 4
Not important line 5
**Another important line 6**
Not important line 7
Name: Block of line 1
Data: Block of line 2
**Important line 3**
Not important line 4
Not important line 5
**Another important line 6**
Not important line 7
Name: Block of line 1
Data: Block of line 2
**Important line 3**
Not important line 4
Not important line 5
**Another important line 6**
Not important line 7
在python中我想读第3行和第6行或当我使用.read()时会使它成为第2行和第5行
我的代码只打印第一个块,所以理想情况下我想循环它:
files = open(fo, 'r')
for i, line in enumerate(files):
if i == 2:
print line
elif i == 5:
print line
break
答案 0 :(得分:0)
files = open(fo, 'r')
for i, line in enumerate(files):
if i%8 == 2:
print line
elif i%8 == 5:
print line
这应该有效,这需要i
mod 8,因为看起来每个块长8行。不是最好的解决方案,您应该将数据保持在稍微整洁的格式。
甚至更简单:
files = open(fo, 'r')
for i, line in enumerate(files):
if i%8 == 2 or i%8 == 5:
print line