Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import urllib.request, shelve
- url= 'http://nancymcohen.com/csci133/cpiai.txt'
- file = urllib.request.urlopen(url)
- lines = file.readlines()
- file.close()
- cpi = {}
- for line in lines:
- items = line.decode().split()
- if len(items)>0 and items[0].isdigit():
- cpi[int(items[0])]=[float(item) for item in items[:13]]
- shelf = shelve.open('cpi')
- shelf['cpi']= cpi
- shelf.close()
- def pctIncrease(begin, end):
- return100*(end/begin-1)
- print(' Percent increase in CPI')
- print(' (Jan - Jan)')
- print(' 1 year 5 years 10 years')
- pattern1 = '{0:d}{1:6.1f}'
- pattern2 = '{0:9.1f}'
- for year in range(1914, 2009):
- currentCPI = cpi[year][1]
- pct = pctIncrease(cpi[year-1][1], currentCPI)
- print(pattern1.format(year,pct), end='')
- if year-5>= 1913:
- pct= pctIncrease(cpi[year-5][1], currentCPI)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement