Use one file to search another file, create new file from found lines
<p>Python beginner here. I've looked around and found similar questions but can't quite cobble enough solutions together for my particular problem. I'm trying to take the contents of a text file that contains user input building names:</p>

<pre><code>GRG
FAC
MEZ
</code></pre>

<p>...that correspond to a line in another text file containing their coordinates:</p>

<pre><code>"GRG",621182.082211,3351342.094278
"FAC",621142.826620,3351182.083363
"MEZ",621288.439353,3350971.487382
</code></pre>

<p>...and either append or create a new text file that will look like this:</p>

<pre><code>"Abbrev","Xcoord","Ycoord"
"GRG",621182.082211,3351342.094278
"FAC",621142.826620,3351182.083363
"MEZ",621288.439353,3350971.487382
</code></pre>

<p>...which will get passed to my GIS software and create a point feature class.  </p>

<p>This is what I've got so far, cobbled together from other questions I've found</p>

<pre><code>inputfile = 'C:\\testing\\inputlist.txt'
datafile = file('C:\\testing\\bldglist.txt')

with open(inputfile,'r') as f:
    inputlist = [line.strip() for line in f]
    print inputlist    #I'm only confident up to this point.
    x = len(inputlist)

outputlist = []

for line in datafile:
    while x &gt;= 0:    #basically I want to iterate through my list
        if inputfile[x] in line:
            outputlist.append(line.strip())
            x = x - 1
print outputlist 
#Once I get the above working I'll work on the output
</code></pre>

<p>So far the only thing I've managed to accomplish with this code is to make my computer flip out due to a MemoryError I think. Any help is greatly appreciated.</p>

<p><strong>Edit:</strong><br>
Thanks to Dan, this is the solution I worked out:  </p>

<pre><code>datafile = 'C:\\testing\\bldglist.txt'
inputfile = 'C:\\testing\\inputlist.txt'

with open(inputfile, 'r') as f:
    names = set([line.strip() for line in f])
    print names

outputlist = []

with open(datafile, 'r') as f:
    for line in f:
        name = line.split(',')[0]
        if name[1:-1] in names:
            outputlist.append(line)
        else:
            print "Nothing found"
    print outputlist
</code></pre>

<p><strong>Edit</strong>  </p>

<p>I still have one outstanding problem. I need the <strong>outputlist</strong> to match the order of the <strong>inputlist</strong>. Unfortunately, the output order matches the order the buildings are found in the <strong>datafile</strong>. How can I make the <strong>outputlist</strong> in the same order as the <strong>inputlist</strong>?</p>
