python输入文字字符串、如何提取某个汉字_如何只读取文本文件中某个字符串之后的行?...

当您到达要开始的行时,只需启动另一个循环:for files in filepath:

with open(files, 'r') as f:

for line in f:

if 'Abstract' in line:

for line in f: # now you are at the lines you want

# do work

文件对象是它自己的迭代器,因此当我们到达包含抽象的行时,我们将继续从该行进行迭代,直到使用迭代器为止。

一个简单的例子:gen = (n for n in xrange(8))

for x in gen:

if x == 3:

print("starting second loop")

for x in gen:

print("In second loop",x)

else:

print("In first loop", x)

In first loop 0

In first loop 1

In first loop 2

starting second loop

In second loop 4

In second loop 5

In second loop 6

In second loop 7from itertools import dropwhile

for files in filepath:

with open(files, 'r') as f:

dropped = dropwhile(lambda _line: "Abstract" not in _line, f)

next(dropped,"")

for line in dropped:

print(line)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值