search a file for text using input from another file with a twist [Python]
<p>I want to use a queryfile.txt as the source file, which will be used for searching and matching each line to a datafile.txt. But the datafile.txt has a different structure.</p>

<p>queryfile.txt should look like this:</p>

<pre><code>Gina Cooper

Asthon Smith

Kim Lee
</code></pre>

<p>while the datafile.txt looks like this:</p>

<pre><code>Gina Cooper

112 Blahblah St., NY

Leigh Walsh

09D blablah, Blah

Asthon Smith

another address here

Kim Lee

another address here
</code></pre>

<p>I need to get the names AND the line after it. Here's the code to get matching names in both files, which is a modified code from dstromberg (<a href="http://stackoverflow.com/a/19934477">http://stackoverflow.com/a/19934477</a>):</p>

<pre><code>with open('querfile.txt', 'r') as input_file:
    input_addresses = set(names.rstrip() for names in input_file)

with open('datafile.txt', 'r') as data_file:
    data_addresses = set(names.rstrip() for names in data_file)

with open('names_address.txt', 'w') as output:
    names_address=("\n".join(input_addresses.intersection(data_addresses)))
    output.write(names_address)
</code></pre>

<p>In summary, what I want to see in my outfile (names_address.txt) are the names PLUS the addresses corresponding to their names, which is basically the next line. I just started playing with python a month ago and I believe I am stuck.Thanks for the help.</p>

<p>Loop through the options instead and then you can just grab the next index:</p>

<pre><code>for i in range(len(data_addresses):
  for entry in input_addresses:
    if entry==data_addresses[i]:
      output.write(data_address[i] + data_address[i+1])
</code></pre>

<p>This might not have great time complexity, but your data set appears </p>

python