# Read # https://storage.googleapis.com/cs1111/practice/books.csv # from the Internet. Do not save to your computer # # Write the following functions # # 1. load_books(inventory, url) # - read the book inventory from a given url # - record the book information in an inventory dict # in the following format, for example: # {'Intro to Python': # {'year': 2020, 'price': 60, 'quantity': 85}, # 'Python - banana': # {'year': 2018, 'price': 64, 'quantity': 100}, # 'Python Master': # {'year': 2018, 'price': 70, 'quantity': 59}, # 'Python crash course': # {'year': 2016, 'price': 40, 'quantity': 29}} # - you may assume the inventory dict is initially empty # # 2. write_inventory(inventory, filename) # - write book information from the inventory dict # to a given file name # (you may format the output file as you wish) import urllib.request def load_books(inventory, url): # open the specified URL to read # store the response object received from the server in a variable stream = urllib.request.urlopen(url) is_header = True for line in stream: if is_header: # get header line headers = line.decode("UTF-8").strip().split(",") for i in range(len(headers)): headers[i] = headers[i].strip() print('headers = ', headers) is_header = False else: book_info = {} # get content columns = line.decode("UTF-8").strip().split(',') for i in range(1, len(columns)): book_info[headers[i]] = columns[i].strip() book_inventory[columns[0].strip()] = book_info print(book_inventory) def print_inventory_to_file1(inventory, filename): with open(filename, "w") as outfile: # must use "w" mode for title, book_info in inventory.items(): line = title + " " for k_info, v_info in book_info.items(): line += v_info # print() takes any data type, must specify an output file print(line, file=outfile) def print_inventory_to_file2(inventory, filename): outfile = open(filename, "w") # must use "w" mode for title, book_info in inventory.items(): line = title + " " for k_info, v_info in book_info.items(): line += v_info # print() takes any data type, must specify an output file print(line, file=outfile) outfile.close() def write_inventory_to_file1(inventory, filename): with open(filename, "w") as outfile: # must use "w" mode for title, book_info in inventory.items(): line = title + " " for k_info, v_info in book_info.items(): line += v_info # write() takes string, need to add \n outfile.write(line + '\n') def write_inventory_to_file2(inventory, filename): outfile = open(filename, "w") # must use "w" mode for title, book_info in inventory.items(): line = title + " " for k_info, v_info in book_info.items(): line += v_info # write() takes string, need to add \n outfile.write(line + '\n') outfile.close() url = "https://storage.googleapis.com/cs1111/practice/books.csv" book_inventory = {} load_books(book_inventory, url) print_inventory_to_file1(book_inventory, "url_print_inventory1.csv") print_inventory_to_file2(book_inventory, "url_print_inventory2.csv") write_inventory_to_file1(book_inventory, "url_write_inventory1.csv") write_inventory_to_file2(book_inventory, "url_write_inventory2.csv")