私は2つのテキストファイルを持っています:
1)citys.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>
citys.txtの各エンティティは<キー>です。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>"
2つのテキストファイルをチェックする方法を知っています(「master」で「city」を見つけてください)...しかし、都市を見つけたら、master.txtでとコンテキストを印刷する方法の一部に固執しています!