Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # More complex datasets
- # THIS IS WHERE WE LEFT OFF LAST TIME...
- import urllib.request
- import json
- # this module needs to be installed separately
- # in PyCharm you can install the package if its not found!
- import var_dump as vd
- # get internet data
- url = 'https://edu.frostbit.fi/api/events/en'
- req = urllib.request.Request(url)
- raw_data = urllib.request.urlopen(req).read().decode("UTF-8")
- # the needed data from internet is now in the "data" -variable!
- data = json.loads(raw_data)
- # vd.var_dump(data)
- # loop through each event we happen to have this time
- for event in data:
- print(event['name'])
- categories = ", ".join(event['categories'])
- print(categories)
- print()
- # NEW VERSION
- import urllib.request
- import json
- # this module needs to be installed separately
- # in PyCharm you can install the package if its not found!
- import var_dump as vd
- # get internet data
- url = 'https://edu.frostbit.fi/api/events/en'
- req = urllib.request.Request(url)
- raw_data = urllib.request.urlopen(req).read().decode("UTF-8")
- # the needed data from internet is now in the "data" -variable!
- data = json.loads(raw_data)
- # vd.var_dump(data)
- # ask the user for a search word (a simple version of Google in this case)
- search_word = input("What kind of events you are looking for?\n")
- for event in data:
- address_text = event['address']['street_address']
- postal_code = event['address']['postal_code']
- categories = ", ".join(event['categories'])
- # if the user's search word is not included in the event
- # skip this event => continue
- if search_word not in categories:
- continue
- # if the search word is found, print normally
- print(event['name'])
- print(f"{address_text} {postal_code}")
- print(categories)
- print()
- # the weather data example, NEW FILE
- import json
- import urllib.request
- import var_dump as vd
- url = "https://edu.frostbit.fi/api/weather/"
- req = urllib.request.Request(url)
- raw_data = urllib.request.urlopen(req).read().decode("UTF-8")
- weather = json.loads(raw_data)
- vd.var_dump(weather)
- # helper variables, MODIFY THESE IN THE LOOP, IF-statements etc.
- strongest_wind = 0
- weakest_wind = 0
- strongest_wind_city = ""
- weakest_wind_city = ""
- # go through each city
- for city in weather:
- print(city['location'])
- print(city['wind'])
- print()
- # finally print the results as needed (check problem description for actual format)
- print(strongest_wind_city)
- print(strongest_wind)
- # OUR FIRST FUNCTION
- # functions.py -file:
- # our functions file
- def print_title():
- print("Welcome to our app!")
- print("-------------------")
- print("Please follow the instructions.")
- print()
- def combine_text(first, last, age):
- print(f"{first} {last}, {age} years old.")
- # the actual application, another file
- from functions import *
- print("START")
- print_title()
- print("END")
- combine_text("Test", "Developer", 31)
Add Comment
Please, Sign In to add comment