Reputation: 35
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:
GRG
FAC
MEZ
...that correspond to a line in another text file containing their coordinates:
"GRG",621182.082211,3351342.094278
"FAC",621142.826620,3351182.083363
"MEZ",621288.439353,3350971.487382
...and either append or create a new text file that will look like this:
"Abbrev","Xcoord","Ycoord"
"GRG",621182.082211,3351342.094278
"FAC",621142.826620,3351182.083363
"MEZ",621288.439353,3350971.487382
...which will get passed to my GIS software and create a point feature class.
This is what I've got so far, cobbled together from other questions I've found
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 >= 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
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.
Edit:
Thanks to Dan, this is the solution I worked out:
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
Edit
I still have one outstanding problem. I need the outputlist to match the order of the inputlist. Unfortunately, the output order matches the order the buildings are found in the datafile. How can I make the outputlist in the same order as the inputlist?
Upvotes: 1
Views: 1572
Reputation: 1889
First of all, store the names in a set, so you don't have to iterate over a list for every line in the second file:
with open(inputfile, 'r') as f:
names = set([line.strip() for line in f])
Then you can build your output list. You can of course also write it directly to the output file, if you don't need to do any more processing on the data:
outputlist = []
with open(datafile, 'r') as f:
for line in f:
name = line.split(',')[0]
# The string slicing is to remove the "" that surrounds the name.
if name[1:-1] in names:
outputlist.append(line)
Upvotes: 1