I have a file in which i provide steps and then based on the steps the content to follow.
Here is the textfile that i read in:
[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
I already have a working model in reading the file, and then assignning the correct content to the correct header (e.g. the url to the header WebAdress). However, what i want to do is follow the looping based on the steps.
Code to process the data:
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
First, create a dictionary mapping the name of a step to a number, like this:
(Granted, this is a pretty ugly line of Python, but it seems to work)
Now, you can sort the items in your dict by those steps:
And iterate over those items: