0

我有一个文件,我在其中提供步骤,然后根据要遵循的内容的步骤。这是我读入的文本文件:

[Steps]
step1 = WebAddress
step2 = Tab
step3 = SecurityType
step4 = Criteria
step5 = Date
step6 = Click1
step7 = Results
step8 = Download
[data]
WebAddress___________________________ Destination___________ Tab_____________ SecurityType___________________________________________________ Criteria___ Date_______ Click1_ Results_ Download    
https://mbsdisclosure.fanniemae.com/  q:\\%s\\raw\\fnmapool  Advanced Search  Interim MBS: Single-Family                                      Issue Date  09/01/2012  Search  100      CSV XML
https://mbsdisclosure.fanniemae.com/  q:\\%s\\raw\\fnmapool  Advanced Search  Preliminary Mega: Fannie Mae/Ginnie Mae backed Adjustable Rate  Issue Date  09/01/2012  Search  100      CSV XML
https://mbsdisclosure.fanniemae.com/  q:\\%s\\raw\\fnmapool  Advanced Search  Preliminary Mega: Fannie Mae/Ginnie Mae backed Fixed Rate       Issue Date  09/01/2012  Search  100      CSV XML

我已经有一个读取文件的工作模型,然后将正确的内容分配给正确的标头(例如标头 WebAdress 的 url)。但是,我想要做的是按照步骤循环。处理数据的代码:

from itertools import groupby
count =0
file_name = "FNMA.tbl"
with open(file_name) as f:
      pre_data,post_data =[s.strip() for s in (f.read()).split("[data]")]
post_data_lines = post_data.splitlines()
headers = post_data_lines[0].split()
headers2 = [s.replace("_"," ").strip() for s in headers]
for line in post_data_lines[1:]:
    tmpline  = []
    pos = 0
    for itm in headers:
        tmpline.append(line[pos:pos+len(itm)])
        pos += len(itm)+1
    myDict= dict(zip(headers2,tmpline))
    count += 1
    for key, group in groupby(myDict.iteritems(), lambda x: x[0]):
        for thing in group:
            print "step: %s header: %s" % (thing[1], key)
    print "Finished processing row %s" % count
4

1 回答 1

0

首先,创建一个将步骤名称映射到数字的字典,如下所示:

steps = dict((step.split()[2], pos) 
        for (pos, step) in enumerate(pre_data.splitlines()[1:]))

(当然,这是一条非常丑陋的 Python 行,但它似乎可以工作)

现在,您可以通过以下步骤对 dict 中的项目进行排序:

sorted_items = sorted(myDict.items(), 
        key=lambda item: steps[item[0]] if item[0] in steps else 999)

并遍历这些项目:

for key, thing in sorted_items:
    print "step: %s header: %s" % (thing, key)
于 2012-09-06T15:34:12.997 回答