def getCityCodeDict(): """ A function to read in database table create a dictionary with the key coming from the first column. The dictionary value an Airport() object. The dictionary is returned. Each row of the table contains 2 strings in double quotes. """ table = 'airport' connection = openConnection() curs = connection.cursor() sqlcmd = "SELECT * FROM " + table d = {} curs.execute(sqlcmd) for row in curs.fetchall(): airport = airlineClasses.Airport() airport.cityCode = row[0] airport.city = row[1] d[airport.cityCode] = airport curs.close() connection.close() return d
def getCityCodeDict(): """ A function to read in a CSV text file and create a dictionary with the key coming from the first field. The dictionary value an Airport() object. The dictionary is returned. Each line of the CSV file contains 2 strings in double quotes. The double quotes and \n are removed from each line before insertion into the dictionary """ dictionary = {} for input in open(filename1, 'r'): if input: input = input.rstrip() # remove the newline input = input.replace('"', '') # replace double quotes with null input = input.split(',') # split at the comma airport = airlineClasses.Airport() # create new object airport.cityCode = input[0] # assign into new object airport.city = input[1] dictionary[airport.cityCode] = airport # store in dictionary return dictionary # Part C