#! python3
# P2_readCensusExcel.py - Tabulates population and number of census tracts for
# each county.
#
# Note:
# - The censuspopdata.xlsx workbook can be downloaded from
#   https://nostarch.com/automatestuff/

import openpyxl, pprint
print("Opening workbook...")
wb = openpyxl.load_workbook("censuspopdata.xlsx")
sheet = wb["Population by Census Tract"]
countyData = {}

# Fill in countyData with each county's population and tracts.
print("Reading rows...")
for row in range(2, sheet.max_row + 1):
    # Each row in the spreadsheet has data for one census tract.
    state = sheet['B' + str(row)].value
    county = sheet['C' + str(row)].value
    pop = sheet['D' + str(row)].value

    # Make sure the key for this state exists.
    countyData.setdefault(state, {})
    # Make sure the key for this county in this state exists.
    countyData[state].setdefault(county, {"tracts": 0, "pop": 0})

    # Each row represents one census tract, so increment by one.
    countyData[state][county]["tracts"] += 1
    # Increase the county pop by the pop in this census tract.
    countyData[state][county]["pop"] += int(pop)

# Open a new text file and write the contents of countyData to it.
print("Writing results...")
resultFile = open("census2010.py", 'w')
resultFile.write("allData = " + pprint.pformat(countyData))
resultFile.close()
print("Done.")

@@ -8,34 +8,30 @@
 on3%0A
-# P2_readC
+%22%22%22Read c
 ensus
+ 
 Excel
-.py - 
+%0A%0A
 Tabu
@@ -78,18 +78,16 @@
 acts for
-%0A#
  each co
@@ -96,21 +96,20 @@
 ty.%0A
-#%0A# 
+%0A
 Note:%0A
-# -
+    *
  The
@@ -163,12 +163,8 @@
 from
-%0A#  
  htt
@@ -197,16 +197,38 @@
 stuff/%0A%0A
+%22%22%22%0A%0A%0Adef main():%0A    
 import o
@@ -243,16 +243,20 @@
  pprint%0A
+    
 print(%22O
@@ -276,16 +276,20 @@
 ok...%22)%0A
+    
 wb = ope
@@ -330,16 +330,20 @@
 .xlsx%22)%0A
+    
 sheet = 
@@ -375,16 +375,20 @@
 Tract%22%5D%0A
+    
 countyDa
@@ -396,16 +396,20 @@
 a = %7B%7D%0A%0A
+    
 # Fill i
@@ -463,16 +463,20 @@
 tracts.%0A
+    
 print(%22R
@@ -492,16 +492,20 @@
 ws...%22)%0A
+    
 for row 
@@ -536,24 +536,28 @@
 w + 1):%0A    
+    
 # Each row i
@@ -609,16 +609,20 @@
 ct.%0A    
+    
 state = 
@@ -649,24 +649,28 @@
 %5D.value%0A    
+    
 county = she
@@ -698,16 +698,20 @@
 lue%0A    
+    
 pop = sh
@@ -737,16 +737,20 @@
 .value%0A%0A
+    
     # Ma
@@ -780,32 +780,36 @@
 s state exists.%0A
+    
     countyData.s
@@ -829,24 +829,28 @@
 te, %7B%7D)%0A    
+    
 # Make sure 
@@ -891,24 +891,28 @@
 ate exists.%0A
+    
     countyDa
@@ -966,16 +966,20 @@
 %22: 0%7D)%0A%0A
+    
     # Ea
@@ -1035,24 +1035,28 @@
 by one.%0A    
+    
 countyData%5Bs
@@ -1084,16 +1084,20 @@
 %22%5D += 1%0A
+    
     # In
@@ -1151,24 +1151,28 @@
  tract.%0A    
+    
 countyData%5Bs
@@ -1205,16 +1205,20 @@
 t(pop)%0A%0A
+    
 # Open a
@@ -1276,16 +1276,20 @@
  to it.%0A
+    
 print(%22W
@@ -1308,16 +1308,20 @@
 ts...%22)%0A
+    
 resultFi
@@ -1352,16 +1352,20 @@
 %22, 'w')%0A
+    
 resultFi
@@ -1416,16 +1416,20 @@
 yData))%0A
+    
 resultFi
@@ -1439,16 +1439,20 @@
 close()%0A
+    
 print(%22D
@@ -1454,12 +1454,52 @@
 nt(%22Done.%22)%0A
+%0A%0Aif __name__ == '__main__':%0A    main()%0A

Update P2_readCensusExcel.py added docstring and wrapped in main() function