나는 단계를 제공 한 파일을 가지고 있으며 따라야 할 내용을 기반으로합니다.단계별/순차적 파이썬으로 반복하기
[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) 올바른 헤더에 대한 올바른 내용을 assignning의 : 여기에 내가에서 읽은 TEXTFILE입니다. 그러나, 내가하고 싶은 것은 단계를 기반으로 루핑을 수행하는 것입니다. 코드는 데이터를 처리하는 :
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
당신은()'하나 개의 분할 위치 (' "순이 CD".split에 여러 공간을 축소 할 열 패딩,'.split로 _''사용할 필요가 없습니다 ()'->'[ "ab", "cd"]'), 사용자 정의 문자를 strip (' "ab____".strip ('_')'->' "ab"' –
@MartijnPieters : '_ 고급 _ 검색'과 같은 항목을 따옴표로 표시 할 필요가 없도록 '_'패딩이 열 너비 정보에 사용되고 있다고 생각합니다. [나는 그들에게 인용 만하고있다.] – DSM
@DSM : 실로; 여기서는 CSV를 사용하여 따옴표 처리를 특수 모듈에 맡깁니다. –