# 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 opening URL import urllib.request # prepare URL to be opened url = 'https://storage.googleapis.com/cs1111/examples/file/todo.csv' # alternatively, we can specify the path and leave filename # 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 =', stream) # http.client.HTTPResponse object print('type of stream =', type(stream)) # 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") # read the entire stream, decode it, then print # print(stream.read().decode('utf-8')) # Alternatively, instead of reading the entire stream, # we can get each line in the stream, then decode the line. # 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)