나는 긁어 낸 문자열 목록이 있습니다. 문자열을 그룹으로 묶은 다음 기둥 형 데이터로 다시 고칩니다. 그러나 변수 제목은 각 그룹에 대해 존재하지 않습니다. 내가 한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는 딕테이션 목록을 원주 형 데이터로 변환하는 것이지만,이 시점에서 필자는 내 접근 방식이 지나치게 복잡하고 좀 더 우아한 것을 놓치고 있다고 느낍니다. 나는 어떤 지침을 주셔서 감사합니다.
알려진입니까? 즉, 다음과 같은 필드가 있음을 알고 계십니까? '['Intake Received Date ','Intake ID ','Allegation Category ','Sub Categories ','Investigation Finding ']' 해당 필드 사이의 데이터가 숫자로 고정되어 있습니까? – dawg
두 질문에 모두 동의합니다. –