[英]Printing the next line after a word matches between two TXT files
I was able to find a stack-overflow code that already existed on creating a list and comparing to a txt file which has been great.我能够找到一个在创建列表时已经存在的堆栈溢出代码,并与一个很棒的 txt 文件进行比较。 However I want to print the next line in the txt file as well once the word matches I tried using
(next (f), end "")
and print(''.join(islice(f, 3)))
but I am unable to get the results I want.但是,一旦我尝试使用
(next (f), end "")
和print(''.join(islice(f, 3)))
尝试匹配单词,我也想打印 txt 文件中的下一行,但我是无法得到我想要的结果。
Below is the code.下面是代码。 Again I am taking no credit for the code, it is from this original stack overflow post How to search a text file for a specific word in Python
我再次不相信代码,它来自这个原始堆栈溢出帖子How to search a text file for a specific word in Python
import os
os.chdir(r"xx")
def createlist():
items = []
with open('phrases.txt') as input:
for line in input:
items.extend(line.strip().split(','))
return items
print(createlist())
word_list = createlist()
with open('text.txt') as f
for word in (sum([x.split() for x in f.read().split('\n')], [])):
if word in word_list:
print (word)
else:
StopIteration
with open('text.txt', 'r') as f:
for line in f:
for word in line.strip().split():
if word in word_list:
print (word)
print (next(f))
else:
StopIteration
To display next line from a file opened as msg
, use next(msg)
within a for line in msg:
loop.要显示作为
msg
打开的文件的下一for line in msg:
使用next(msg)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.