''' DS2000 Fall 2024 Sample code from class -- reading from a CSV file into a dictionary Goal today: answer the question: given a state, how many students from that state at Northeastern? ''' import csv FILENAME = "neu_geography.csv" def csv_to_dict(filename): ''' parameter: string, filename for a csv file returns: dictionary where key = row[0] and value = row[1] for every row does: creates a dictionary, iterates over the csv file, and adds a new key/value pair for each row (key at row[0], value at row[1]) ''' data = {} with open(filename, "r") as infile: csvfile = csv.reader(infile, delimiter = ",") next(csvfile) for row in csvfile: key = row[0] value = int(row[1]) data[key] = value return data def main(): # gather data -- read in from file to dictionary dct = csv_to_dict(FILENAME) # prompt the user for a state, print # students from that state state = input("What state?\n") # state is a key, here we retrieve the value at that key count = dct[state] print("There are", count, "students from", state) main()