按列表顺序从文件返回行

时间:2012-03-25 17:49:48

标签: python

我试图将类似问题的解决方案放在一起,但却惨遭失败。我还不太了解Python :(

我有一个inputlist包含特定顺序的元素:["GRE", "KIN", "ERD", "KIN"]

我有datafile包含元素,以及其他数据ex:

"ERD","Data","Data"...  
"KIN","Data","Data"...  
"FAC","Data","Data"...  
"GRE","Data","Data"...  

我需要按照outputlist中出现的顺序创建一个datafile,其中包含inputlist中的行。

以下代码按照outputlist中显示的顺序返回datafile,这不是预期的行为......: - \

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

outputlist = []

with open(datafile, 'r') as f:
    for line in f:
        name = line.split(',')[0]
        if name[1:-1] in names: 
            outputlist.append(line)
    output = open(outputfile, 'w')
    output.writelines(outputlist)

如何让它以正确的顺序返回列表?在此先感谢您的帮助: - )

修改

感谢奥斯卡,这是我实施的解决方案:

datafile = 'C:\\testing\\bldglist.txt'
inputfile = 'C:\\testing\\inputlist.txt'
outputfile = "C:\\testing\\output.txt"

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

def outputList(inputlist, datafile, outputfile):
    d = {}
    with open(datafile, 'r') as f:
        for line in f:
            line = line.strip()
            key = line.split(',')[0]
            d[key] = line
    with open(outputfile, 'w') as f:
        f.write('"Abbrev","Xcoord","Ycoord"\n')
        for key in inputlist:
            f.write(d[key] + '\n')

outputList(inputlist, datafile, outputfile)

3 个答案:

答案 0 :(得分:5)

这是一个简单的解决方案。它将整个输入文件作为首字母的字典读取到内存中:line。然后在写入顺序中编写行很容易。

如果文件非常大(千兆字节)或者您没有大量内存,还有其他方法。但他们并不是那么好。

我没有测试过这个。

import csv

data = {}
with open(datafile) as f:
    for line in csv.reader(f):
        data[line[0]] = line

with open(outputfile, "w") as f:
    f = csv.writer(f)
    for entry in inputlist:
        f.writerow(data[entry])

答案 1 :(得分:1)

假设具有以下格式的数据文件:

"ERD","Data","Data"...  
"KIN","Data","Data"...  
"FAC","Data","Data"...  
"GRE","Data","Data"... 

试试这个解决方案:

def outputList(inputlist, datafile, outputfile):
    d = {}
    with open(datafile, 'r') as f:
        for line in f:
            line = line.lstrip()
            key = line.split(',')[0]
            d[key] = line
    with open(outputfile, 'w') as f:
        for key in inputlist:
            f.write(d[key])

像这样使用:

outputList(['"GRE"', '"KIN"', '"ERD"', '"KIN"'],
           '/path/to/datafile',
           '/path/to/outputfile')

它将使用预期的顺序写入输出文件。

答案 2 :(得分:0)

1)创建一个包含您要映射到的元素的列表。在这种情况下,["GRE", "KIN", "ERD", "FAC"]

2)读取文件并映射(使用列表字典)第一个元素。

3)输出到文件。

import csv

out_index=["GRE", "KIN", "ERD", "FAC"]
d={}
with open('/Users/andrew/bin/SO/abcd.txt','r') as fr:
    for e in csv.reader(fr):
        if e[0] not in d: d[e[0]]=[]
        for ea in e[1:]:
            d[e[0]].append(ea)

for i in out_index:
    print i,":"
    for e in d[i]:
        print '   ',e

鉴于此示例数据:

"ERD","Data-a1","Data-a2"
"KIN","Data-b1","Data-b2"
"FAC","Data-c1","Data-c2"
"GRE","Data-d1","Data-d2"
"ERD","Data-a3","Data-a4"
"GRE","Data-d3","Data-d4"

输出:

GRE :
    Data-d1
    Data-d2
    Data-d3
    Data-d4
KIN :
    Data-b1
    Data-b2
ERD :
    Data-a1
    Data-a2
    Data-a3
    Data-a4
FAC :
    Data-c1
    Data-c2

完成!