使用一个文件搜索另一个文件,从找到的行创建新文件

时间:2012-03-03 19:57:16

标签: python

Python初学者。我环顾四周,发现了类似的问题,但是对于我的特定问题,我不能完全解决足够的解决方案。我正在尝试获取包含用户输入建筑物名称的文本文件的内容:

GRG
FAC
MEZ

...对应于包含其坐标的另一个文本文件中的一行:

"GRG",621182.082211,3351342.094278
"FAC",621142.826620,3351182.083363
"MEZ",621288.439353,3350971.487382

...并附加或创建一个如下所示的新文本文件:

"Abbrev","Xcoord","Ycoord"
"GRG",621182.082211,3351342.094278
"FAC",621142.826620,3351182.083363
"MEZ",621288.439353,3350971.487382

...将传递给我的GIS软件并创建一个点要素类。

这是我到目前为止所做的,从我发现的其他问题拼凑而成

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

到目前为止,我用这段代码完成的唯一事情就是让我的计算机由于我想到的MemoryError而翻转。非常感谢任何帮助。

修改
感谢Dan,这是我解决的问题:

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

修改

我还有一个突出的问题。我需要输出列表来匹配输入列表的顺序。不幸的是,输出顺序与数据文件中找到建筑物的顺序相匹配。如何以与输入列表相同的顺序创建输出列表

1 个答案:

答案 0 :(得分:1)

首先,将名称存储在一个集合中,这样您就不必遍历第二个文件中每一行的列表:

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

然后您可以构建输出列表。如果您不需要对数据进行任何其他处理,您当然也可以直接将其写入输出文件:

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)