스프 레 드 시트 에서 데 이 터 를 읽 고 데이터 구 조 를 채 웁 니 다.

1491 단어
import openpyxl,pprint

print('opening workbook...')
wb = openpyxl.load_workbook('censuspopdata.xlsx')
sheet = wb.get_sheet_by_name('Population by Census Tract')
countryData = {}
print('Reading rows...')

for row in range(2,sheet.max_row+1):
    state = sheet['B' + str(row)].value
    county = sheet['C' + str(row)].value
    pop = sheet['D' + str(row)].value
    print(state,county,pop)

    countryData.setdefault(state,{})
    countryData[state].setdefault(county,{'tracts':0,'pop':0})
    countryData[state][county]['pop'] += int(pop)

    print('Writing results...')
    resultFile = open('census2010.py','w')
    resultFile.write('allData = '+ pprint.pformat(countryData))
    resultFile.close()
    print('Done.....')
print(countryData)
print(countryData['CA']['San Francisco']['pop'])
  • 결 과 는:
  • opening workbook...
    Reading rows...
    CA San Francisco 2685
    Writing results...
    Done.....
    CA San Francisco 3894
    Writing results...
    Done.....
    CA San Francisco 5592
    Writing results...
    Done.....
    CA San Francisco 4578
    Writing results...
    Done.....
    CA San Francisco 4320
    Writing results...
    Done.....
    CA San Francisco 4827
    Writing results...
    Done.....
    CA San Francisco 5164
    Writing results...
    Done.....
    {'CA': {'San Francisco': {'tracts': 0, 'pop': 31060}}}
    31060
    

    좋은 웹페이지 즐겨찾기