Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- import urllib
- from urllib.request import urlopen
- from datetime import datetime
- def date_format(date: str): # making format of date 'better'
- weekday = date[0:4]
- day = date[4:6]
- month_and_year = date[6:15]
- day_suffix = "th"
- if day == '01' or day == '21' or day == '31':
- suffix = "st"
- elif day == '02' or day == '22':
- suffix = "nd"
- elif day == '03' or day == '23':
- suffix = "rd"
- if date[4] == '0':
- return date[9] + day_suffix
- return weekday + day + day_suffix + month_and_year
- max_date = " "
- max_value = 0
- min_date = ""
- min_value = 10
- url = "https://api.nbp.pl/api/exchangerates/rates/a/chf/2022-01-01/2023-01-01/?format=json"
- with urllib.request.urlopen(url_address) as url:
- data = json.load(url)
- days = []
- for row in data["rates"]:
- day = {"date": row["effectiveDate"], "rate": row["mid"]}
- days.append(day)
- peak = max(days, key=lambda day: day["rate"])
- depression = min(days, key=lambda day: day["rate"])
- max_date = datetime.strptime(peak["date"], '%Y-%m-%d').date().strftime("%a %d %b %Y")
- min_date = datetime.strptime(depression["date"], '%Y-%m-%d').date().strftime("%a %d %b %Y")
- print(date_format(max_date), peak["rate"])
- print(date_format(min_date), depression["rate"])
Advertisement
Add Comment
Please, Sign In to add comment