2014-02-23 2 views
2

나는 긁어 낸 문자열 목록이 있습니다. 문자열을 그룹으로 묶은 다음 기둥 형 데이터로 다시 고칩니다. 그러나 변수 제목은 각 그룹에 대해 존재하지 않습니다. 내가 한Python 목록을 기둥 형 데이터로 변환

'Intake Received Date', 'Intake ID', 'Allegation Category', 'Sub Categories', 'Investigation Finding' 
'9/11/2012', 'CA00325127', 'Infection Control', '', 'Substantiated' 
'5/14/2012', 'CA00310421', 'Quality of Care/Treatment', '', 'Substantiated' 
'8/15/2011', 'CA00279396', 'Quality of Care/Treatment', 'Screening', 'Unsubstantiated' 

우선은 조각으로 목록을 중단했다 :

내 목록 complist을이며 모양은 다음과 같습니다

[u'Intake Received Date:', 
u'9/11/2012', 
u'Intake ID:', 
u'CA00325127', 
u'Allegation Category:', 
u'Infection Control', 
u'Investigation Finding:', 
u'Substantiated', 
u'Intake Received Date:', 
u'5/14/2012', 
u'Intake ID:', 
u'CA00310421', 
u'Allegation Category:', 
u'Quality of Care/Treatment', 
u'Investigation Finding:', 
u'Substantiated', 
u'Intake Received Date:', 
u'8/15/2011', 
u'Intake ID:', 
u'CA00279396', 
u'Allegation Category:', 
u'Quality of Care/Treatment', 
u'Sub Categories:', 
u'Screening', 
u'Investigation Finding:', 
u'Unsubstantiated',] 

을 그리고 내 목표는 다음과 같이 확인하는 것입니다 시작 요소를 기준으로 Intake Received Date

compgroup = [] 
for k, g in groupby(complist, key=lambda x:re.search(r'Intake Received Date', x)): 
    if not k: 
     compgroup.append(list(g)) 


#Intake Received Date was removed, so insert it back to beginning of each list: 
for c in compgroup: 
    c.insert(0, u'Intake Received Date') 


#Create list of dicts to map the preceding titles to their respective data element: 
dic = [] 
for c in compgroup: 
    dic.append(dict(zip(*[iter(c)]*2))) 

다음 st ep는 딕테이션 목록을 원주 형 데이터로 변환하는 것이지만,이 시점에서 필자는 내 접근 방식이 지나치게 복잡하고 좀 더 우아한 것을 놓치고 있다고 느낍니다. 나는 어떤 지침을 주셔서 감사합니다.

+0

알려진입니까? 즉, 다음과 같은 필드가 있음을 알고 계십니까? '['Intake Received Date ','Intake ID ','Allegation Category ','Sub Categories ','Investigation Finding ']' 해당 필드 사이의 데이터가 숫자로 고정되어 있습니까? – dawg

+0

두 질문에 모두 동의합니다. –

답변

1

을 감안할 때 :

data=[u'Intake Received Date:', 
u'9/11/2012', 
u'Intake ID:', 
u'CA00325127', 
u'Allegation Category:', 
u'Infection Control', 
u'Investigation Finding:', 
u'Substantiated', 
u'Intake Received Date:', 
u'5/14/2012', 
u'Intake ID:', 
u'CA00310421', 
u'Allegation Category:', 
u'Quality of Care/Treatment', 
u'Investigation Finding:', 
u'Substantiated', 
u'Intake Received Date:', 
u'8/15/2011', 
u'Intake ID:', 
u'CA00279396', 
u'Allegation Category:', 
u'Quality of Care/Treatment', 
u'Sub Categories:', 
u'Screening', 
u'Investigation Finding:', 
u'Unsubstantiated',] 

귀하의 방법은 꽤 좋다. 나는 그것을 약간 편집했다. 당신은 정규식을 필요로하지 않으며, 당신은 Intake Received Date

에 한번 다시 삽입 할 필요가 없습니다 :

from itertools import groupby 

headers=['Intake Received Date:', 'Intake ID:', 'Allegation Category:', 'Sub Categories:', 'Investigation Finding:'] 
sep='Intake Received Date:' 
compgroup = [] 
for k, g in groupby(data, key=lambda x: x==sep):  
    if not k: 
     compgroup.append([sep]+list(g)) 

print ', '.join(e[0:-1] for e in headers)  

for di in [dict(zip(*[iter(c)]*2)) for c in compgroup]: 
    line=[] 
    for h in headers: 
     try: 
      line.append(di[h]) 
     except KeyError: 
      line.append('*') 
    print ', '.join(line) 

인쇄 : 제목이

Intake Received Date, Intake ID, Allegation Category, Sub Categories, Investigation Finding 
9/11/2012, CA00325127, Infection Control, *, Substantiated 
5/14/2012, CA00310421, Quality of Care/Treatment, *, Substantiated 
8/15/2011, CA00279396, Quality of Care/Treatment, Screening, Unsubstantiated 
+0

이것은 어떤 흡기 ID가 "Screening"을 하위 카테고리로 가지고 있는지에 관한 정보를 잃어 버리게됩니다. – DSM

+0

@DSM : 감사합니다. 당신 말이 맞아요. 결정된 – dawg

관련 문제