我有 2 个文本文件:
1) 城市.txt
San Francisco
Los Angeles
Seattle
Dallas
2)master.txt
Atlanta is chill and laid-back.
I love Los Angeles.
Coming to Dallas was the right choice.
New York is so busy!
San Francisco is fun.
Moving to Boston soon!
Go to Seattle in the summer.
试图获取 output.txt
<main><beg>I love</beg><key>Los Angeles</key><end></end></main>
<main><beg>Coming to</beg><key>Dallas</key><end>was the right choice</end></main>
<main><beg></beg><key>San Francisco</key><end>is fun</end></main>
<main><beg>Go to</beg><key>Seattle</key><end>in the summer</end></main>
city.txt 中的每个实体都是 <key>。master.txt 文件要长得多,所有没有特定城市的行都应该被忽略。他们不按顺序。输出打印出 <key> 和 <beg> & <end> 上下文中的城市(如果有)。
这就是我所拥有的:
with open(master.txt) as f:
master = f.read()
working = []
with open(cities.txt) as f:
for i in (word.strip() for word in f):
if i in master:
print "<key>", i, "</key>"
我知道如何检查两个文本文件(在 'master' 中找到 'city')......但是一旦我找到城市,我就卡在了如何在 master.txt 中打印和上下文的部分!