用python查找连续行

我有一个文档,内容类似于

the line 1 is:
a-b-o 
the line 2 is:
not found
the line 3 is:
not found
the line 4 is:
a-c-k
the line 5 is:
a-d-g
a-h-o
the line 6 is:
a-g-i
the line 7 is:
not found

这种。我想用python得到连续的有内容的那几行,而不是not found的那几行。以上面的为例,我想得到的结果是:

the line 4 is:
a-c-k
the line 5 is:
a-d-g
a-h-o
the line 6 is:
a-g-i

请问大家可以怎么实现,谢谢。
阅读 1.4k
1 个回答

不知道你这文件大不大。如果不大,你可以一次读出,记为txt,然后以"not found"分割txt,特殊判断txt[0]txt[-1];对于剩下的txt[x],如果内容中有大于2个"line"字符串,说明这就是符合要求的,做一做处理,然后输出。
如果文件比较大,可以一部分一部分读取,然后判断连接位置的情况,其余的还是按上述处理

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题