如何只使用python读取某个字符串后的文本文件中的行?(How to only read lines in a text file after a certain string using pyth

编程入门 行业动态 更新时间:2024-10-23 03:19:58
如何只使用python读取某个字符串后的文本文件中的行?(How to only read lines in a text file after a certain string using python?)

使用python,我想读一个字典中的所有文本文件中的特定字符串后面的行。 我想在数千个文本文件中完成此操作。

我能够使用以下代码(从此堆栈溢出答案中获得)识别并打印出特定的字符串('Abstract'):

for files in filepath: with open(files, 'r') as f: for line in f: if 'Abstract' in line: print line;

但是,我该如何告诉python开始阅读仅在字符串之后出现的行?

Using python, I'd like to read to a dictionary all of the lines in a text file that come after a particular string. I'd like to do this over thousands of text files.

I'm able to identify and print out the particular string ('Abstract') using the following code (gotten from this stack overflow answer):

for files in filepath: with open(files, 'r') as f: for line in f: if 'Abstract' in line: print line;

But how do I tell python to start reading the lines that only come after the string?

最满意答案

当您到达您想要开始的线路时,再启动另一个循环:

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

一个文件对象是它自己的迭代器,所以当我们到达带有Abstract的行时,我们继续从该行开始迭代,直到我们已经消耗了迭代器。

一个简单的例子:

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 7

你也可以使用itertools.dropwhile消耗直到你想要的点的行。

from 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)

just start another loop when you reach the line you want to start from :

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

A file object is it's own iterator, so when we reach the line with Abstract in it we continue our iteration from that line until we have consumed the iterator.

A simple example:

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 7

You can also use itertools.dropwhile to consume the lines up to the point you want.

from 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)

更多推荐

本文发布于:2023-07-31 02:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340477.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   文本文件   read   python   file

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!