# Write code to read content from # https://storage.googleapis.com/cs1111/examples/file/todo.csv # then (print) display the file content on screen # Be sure to read from the Internet, # do not download the file to your computer # import a library for openning URL import urllib.request # prepare URL to be opened filename = 'todo.csv' url = 'https://storage.googleapis.com/cs1111/examples/file/' + 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") number_AM_todo = 0 number_PM_todo = 0 # 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) # now, add code to count the number of AM activities # and PM activities and print a todo_dict dictionary that # contains the numbers of activities # {'number_AM_todo': 4, 'number_PM_todo': 7} todo_dict = {'number_AM': 0, 'number_PM': 0} columns = decoded_data.split(',') if 'am' in columns[1] or 'AM' in columns[1]: todo_dict['number_AM'] += 1 elif 'pm' in columns[1] or 'PM' in columns[1]: todo_dict['number_PM'] += 1 print(todo_dict) print('todo_dict =', todo_dict)