# import a library for openning URL import urllib.request # prepare URL to be opened filename = 'arbys.csv' # 'wendys.csv' #'arbys.csv' url = 'http://cs1110.cs.virginia.edu/files/' + filename # let's look at the URL print("url = " + url) # open the specified URL to read # store the response object received from the server in a variable stream = urllib.request.urlopen(url) # print(stream) # http.client.HTTPResponse object # Tips: # - Instead of "open()" when opening a file, # we use "urlopen()" when opening a url # - Once opening a url, we must decode the data # -- unlike reading the file, we can simply read() or readline() print("\n==== Start processing data ==== \n") list_of_store = [] # # read each line from the response object for line in stream: # decode the string using the standard encoding scheme (UTF-8) # then, strip leading and tailing spaces, and split the string using "," # decoded_data = line.decode("UTF-8").strip() # print(decoded_data) store_location = line.decode("UTF-8").strip().split(",") # split results in list list_of_store.append(store_location) stores_in_state = {} # Count the number of stores in each state. # Then print a dictionary containing pairs # of states and number of store, where # key is the state and # value is the number store in the state # For example, # {'VA': 106, 'AR': 43, 'NS': 2, 'IN': 175, 'PA': 137, 'MO': 85, 'SC': 69, 'ID': 19, 'MN': 81, 'WV': 39, 'KS': 55, 'ND': 11, 'TN': 106, 'NH': 2, 'NM': 27, 'AZ': 70, 'CT': 8, 'FL': 140, 'NJ': 19, 'SK': 4, 'TX': 132, 'AK': 8, 'IL': 125, 'AB': 27, 'MB': 2, 'HI': 3, 'LA': 27, 'MA': 4, 'NY': 73, 'NB': 2, 'SD': 19, 'MI': 173, 'KY': 117, 'OK': 91, 'CA': 98, 'WI': 90, 'WY': 16, 'CO': 61, 'WA': 55, 'BC': 8, 'MD': 43, 'MS': 23, 'OH': 268, 'NE': 44, 'IA': 54, 'OR': 29, 'ON': 29, 'UT': 66, 'AL': 97, 'MT': 19, 'NC': 130, 'ME': 5, 'NV': 26, 'GA': 143, 'DE': 15} for store_info in list_of_store: # print(store_info) # keep track how many stores are in the same state state = store_info[3].strip() if state in stores_in_state: stores_in_state[state] += 1 else: stores_in_state[state] = 1 print(stores_in_state) # What can we do with this dataset? # - Count how many days the weather was clear # - Compute the average temperature of the day # - Find patterns and estimate likelyhood