I am following the example project under Chapter 13 (Reading Data from a spreadsheet). I have created the census2010.py code and save it in my documents. When I run the quote, I'm getting this key error.
KeyError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_35244/1301298642.py in <module>
26
27 import census2010
---> 28 census2010.allData['AK']['Anchorage']
29 anchoragePop = census2010.allData['AK']['Anchorage']['pop']
30 print('The 2010 population of Anchorage was ' + str(anchoragePop))
KeyError: 'AK'
This is the current code that I have running on the interactive shell.
import openpyxl, pprint
import os
os.chdir('C:\\users\\brian\\documents')
print('Opening workbook...')
wb = openpyxl.load_workbook('censuspopdata.xlsx')
sheet = wb['Population by Census Tract']
countyData = {}
# TODO: Fill in countyData with each county's population and tracts.
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
countyData.setdefault(state, {})
countyData[state].setdefault(county, {'tracts': 0, 'pop': 0})
countyData[state][county]['tracts'] += 1
countyData[state][county]['pop'] += int(pop)
print('Writing results...')
resultFile = open('census2010.py', 'w')
resultFile.write('allData = ' + pprint.pformat(countyData))
resultFile.close()
print('Done.')
import census2010
census2010.allData['AK']['Anchorage']
anchoragePop = census2010.allData['AK']['Anchorage']['pop']
print('The 2010 population of Anchorage was ' + str(anchoragePop))
Not sure how I can get my data from the excel file that I saved also in my documents.