""" Felix Muzny DS 2000 Lecture 15 November 1, 2024 Stater code Reading in files as dictionaries Lists of dictionaries """ import csv import matplotlib.pyplot as plt FILENAME = "ds2000_staff.csv" AGE_COL = "age" NUM_BINS = 10 def read_file_dict(filename): """ Reads the file in as a single dictionary, ignoring the first (header) row :param filename: string location of the file :return: dictionary mapping strings to ints """ data = {} with open(filename, "r") as file: # ignore the headers headers = file.readline() # read the rest of the lines # for line in file: # OUR CODE HERE return data # partially provided def read_file_as_list_of_dicts(filename): """ Reads the given file as a list of dictionaries, using the first row of headers as key names :param filename: string location of file :return: list of dicts mapping strings to strings """ data = [] with open(filename, "r") as file: reader = csv.DictReader(file) # OUR CODE HERE # return the data! return data def make_age_hist(data, age_col, bins): """ Make a histogram of the ages in the data :param data: list of dictionaries :param age_col: string name of column key associated with ages data :param bins: int number of bins for the histogram :return: none """ # get the ages as integers # this is like accessing the ages column from the file print("we'll implement this!") def main(): print("Lecture 15 - reading files into dicts") # read the file into a dictionary data = read_file_dict(FILENAME) print(data) main()