Skip to content

Commit c7610af

Browse files
Lot of error solving done
1 parent bda0592 commit c7610af

File tree

7 files changed

+3212
-0
lines changed

7 files changed

+3212
-0
lines changed

Chapter12–WorkingwithExcelSpreadsheets/census2010.py

Lines changed: 3143 additions & 0 deletions
Large diffs are not rendered by default.
2.14 MB
Binary file not shown.
9.67 KB
Binary file not shown.
857 KB
Binary file not shown.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#! /usr/bin/python3.5
2+
# coding: utf-8
3+
4+
"""
5+
6+
Author: Samrat Banerjee
7+
Dated: 25/08/2018
8+
Description: Project: Tabulates population and number of census tracts for each county
9+
10+
"""
11+
12+
import openpyxl,pprint
13+
print('Opening workbook...')
14+
wb=openpyxl.load_workbook('/home/samrat/LearningAutomationwithPython/Chapter12–WorkingwithExcelSpreadsheets/censuspopdata.xlsx')
15+
sheet = wb.get_sheet_by_name('Population by Census Tract')
16+
countyData={}
17+
18+
# Fill in countyData with each county's population and tracts.
19+
print('Reading rows...')
20+
for row in range(2,sheet.max_row+1):
21+
# Each row in the spreadsheet has data for one census tract.
22+
state=sheet['B'+str(row)].value
23+
county=sheet['C'+str(row)].value
24+
pop=sheet['D'+str(row)].value
25+
26+
# Make sure the key for this state exists
27+
countyData.setdefault(state,{})
28+
# Make sure the key for this county in this state exists.
29+
countyData[state].setdefault(county,{'tracts':0,'pop':0})
30+
# Each row represents one census tract, so increment by one.
31+
countyData[state][county]['tracts']+=1
32+
# Increase the county pop by the pop in this census tract.
33+
countyData[state][county]['pop']+=int(pop)
34+
35+
# Open a new text file and write the contents of countyData to it.
36+
print('Writing results...')
37+
resultFile=open('census2010.py','w')
38+
resultFile.write('allData = '+pprint.pformat(countyData))
39+
resultFile.close()
40+
print('Done.')
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#! /usr/bin/python3.5
2+
# coding: utf-8
3+
4+
"""
5+
6+
Author: Samrat Banerjee
7+
Dated: 25/08/2018
8+
Description: Project: Corrects costs in produce sales spreadsheet.
9+
10+
"""
11+
12+
import openpyxl
13+
14+
wb=openpyxl.load_workbook('/home/samrat/LearningAutomationwithPython/Chapter12–WorkingwithExcelSpreadsheets/produceSales.xlsx')
15+
sheet = wb.get_sheet_by_name('Sheet')
16+
17+
# The produce types and their updated prices
18+
PRICE_UPDATES = {'Garlic': 3.07,
19+
'Celery': 1.19,
20+
'Lemon': 1.27}
21+
22+
# Loop through the rows and update the prices.
23+
24+
for rowNum in range(2, sheet.max_row):
25+
# skip the first row
26+
produceName = sheet.cell(row=rowNum, column=1).value
27+
if produceName in PRICE_UPDATES:
28+
sheet.cell(row=rowNum, column=2).value = PRICE_UPDATES[produceName]
29+
wb.save('updatedProduceSales.xlsx')
Binary file not shown.

0 commit comments

Comments
 (0)