简体   繁体   English

在以下行 python

[英]finding a word on the following line python

I'm searching through a text file for a certain string then looking to find another string following that string, it could be on the next line or further down the document.我在一个文本文件中搜索某个字符串,然后寻找该字符串后面的另一个字符串,它可能在下一行或文档的更下方。 I currently have我目前有

so an example text output would like所以一个示例文本 output 想

there is a word1. then there is some more text. 
then we are looking for word2 = apple. 

i'm looking to return the word 'apple' + word1.我正在寻找返回单词'apple'+ word1。 However word2= can be on the next line or further down the document.但是 word2= 可以在下一行或文档的更下方。 i've managed to do the below but this only works if its on the next line.我已经设法做到以下几点,但这只有在下一行时才有效。 not if it was on line 3,4, 5 etc. can anyone help?如果它在 3,4、5 等线上,没有人可以帮忙吗?

if 'word1' in line and 'word2' not in line:        
    nextLine = next(f)
    pattern = re.match('(?:word2=|word2 =)([a-z0-9_])+',nextLine) 
    if pattern:    
        print('word1', pattern)

If I get it right, I made an example for you:如果我做对了,我为你做了一个例子:

string = """

there is a word1. then there is some more text. 
then we are looking for word2 = apple. 


there is a word1. then there is some more text. 
then we are looking for word2 = orange. 



there is a word1. then there is some more text. 
then there is some more text. 
then there is some more text. 
then we are looking for word2= peer. 
"""


import re
result = re.findall(".*?(word1)[\s\S]*?word2 *=.*?([a-z0-9_]+)", string)
print(result)
# should be [('word1', 'apple'), ('word1', 'orange'), ('word1', 'peer')]

Note: As I am using the whole string to match, my example may not be suitable for big size file.注意:由于我使用整个字符串进行匹配,我的示例可能不适合大文件。

if 'word1' in line and 'word2' not in line: 
while True:       
    nextLine = next(f)
    pattern = re.match('(?:word2=|word2 =)([a-z0-9_])+',nextLine) 
    if pattern:    
        print('word1', pattern)
        break

Not sure it will work dont have access to PC let me know, if not working I'll delete it不确定它是否会工作 无法访问 PC 让我知道,如果不工作我会删除它

beware tough:当心强硬:

Are all infinite loops bad? 所有无限循环都不好吗?

Is while (true) with break bad programming practice? while (true) 是否具有坏的编程习惯?

You should read your complete file in one string, and then try this.您应该在一个字符串中读取完整的文件,然后试试这个。 This will capture word1, and whatever equates to word2 using capturing groups :这将捕获 word1,以及使用捕获组等同于 word2 的任何内容:

(word1)(?:.*[\n\r]?)+word2 ?= ?(\w+)

It is not clear from your question whether we should match word2 = apple or word2=apple (maybe the last time you mentioned word2= it was a typo?), so I included the ?从您的问题中不清楚我们是否应该匹配word2 = appleword2=apple (也许您上次提到word2=这是一个错字?),所以我包括了? character, which will make the spaces optional.字符,这将使空格可选。

If you want your answer in the format apple + word1 , you can do:如果您希望以apple + word1格式给出答案,您可以执行以下操作:

print(pattern.group(1) + " + " + pattern.group(2))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM