I have 2 text files:
cities.txt
San Francisco
Los Angeles
Seattle
Dallas
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>
Each entity in cities.txt is the <key>. The master.txt file is much longer, and all lines without the city in cities.txt should just be ignored. They're not in order. The output prints out the cities in <key> and <beg> & <end> context (if any).
This is what I have:
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 data:
print "<key>", i, "</key>"
I can find the cities in the master file and print them out to a new file, but I'm stuck at how I can also print out the beginning and end contexts for the lines with the cities!