def list_to_dict(input_single_dict): ''' Create a dictionary from a single csv file Inputs: path of the csv file Returns: dictionary ''' # create a new list with the values from csv list_new = read_csv(input_single_dict, True) # define a new empty dict to store the values dict_new = {} # define the name of vars to pass tests names_for_tests = {'age': AGE, 'earnings_per_week': EARNWKE, 'employment_status_code': STATUS, 'ethnicity_code': ETHNIC, 'gender_code': GENDER, 'hours_worked_per_week': HRWKE, 'race_code': RACE} # assign Key + value to each element of a list # iterate through primary key (e.g. hid) for i in range(1, len(list_new)): # assign each home a unique dictionay key key_home = list_new[i][0] # create a dictionary within the dictionary dict_hid = {} # if list contains other sublistsinside if len(list_new[0]) > 2: # iterate through secondary keys (e.g. age) for j in range(1, len(list_new[1])): # key of variable is allways in element 0 of new_list key_of_variable = list_new[0][j] # name of variable name_of_var = names_for_tests[key_of_variable] # get value for each variable and each unique home value_of_variable = list_new[i][j] # create the hid dictuinary for each home dict_hid[name_of_var] = value_of_variable # add the hid dict to the greater dict dict_new[key_home] = dict_hid # if the list has only a key and a value if len(list_new[0]) == 2: value = list_new[i][1] dict_new[key_home] = value # return the dictionary of product and price return dict_new
def initialize_code(filename, need_header = True): ''' Reads the "codes.csv" filename and returns a dict based on the given filename. Inputs: filename - string that represents one of the csv code files need_header - True if the header is needed, false otherwise Returns: dictionary with codes and corresponding val ''' code = read_csv(filename, need_header) d = {code[0]: (code[1]) for (code[0], code[1]) in code[0:]} return d
def build_morg_dict(input_dict): ''' Build a dictionary that holds a set of CPS data Inputs: input_dict: dict An example of input_dict is shown below: {'morg':'data/morg_d14_mini.csv', 'gender_codes':'data/gender_code.csv', 'race_codes':'data/race_code.csv', 'ethnic_codes':'data/ethnic_code.csv', 'employment_codes':'data/employment_status_code.csv'} Returns: dict ''' gender_codes_dict = initialize_code( input_dict[GENDER_CODE]) race_codes_dict = initialize_code( input_dict[RACE_CODE]) ethnic_codes_dict = initialize_code( input_dict[ETHNIC_CODE]) employment_codes_dict = initialize_code( input_dict[EMPLOYMENT_CODE]) morg_dict = {} dataset = read_csv(input_dict[MORG_CODE], True)[1:] for rowdata in dataset: hid_key = rowdata[0] hid_dict = format_row_data(rowdata, gender_codes_dict, race_codes_dict, ethnic_codes_dict, employment_codes_dict) morg_dict[hid_key] = hid_dict return morg_dict
def make_subdict(input_dict): ''' Builds a dictionary that contains the input Morg data and the code files as values with keys of the provided Constants for each attribute (GENDER, RACE, etc) Inputs: input_dict: dict An example of input_dict is shown below: {'morg':'data/morg_d14_mini.csv', 'gender_codes':'data/gender_code.csv', 'race_codes':'data/race_code.csv', 'ethnic_codes':'data/ethnic_code.csv', 'employment_codes':'data/employment_status_code.csv'} Returns: sub_dict: dict ''' data_csv = input_dict['morg'] gen_csv = input_dict['gender_codes'] rc_csv = input_dict['race_codes'] ec_csv = input_dict['ethnic_codes'] emc_csv = input_dict['employment_codes'] csvs = [data_csv, gen_csv, rc_csv, ec_csv, emc_csv] names = ['data', GENDER, RACE, ETHNIC, STATUS] n = len(csvs) sub_dict = {} for n in range(n): f = csvs[n] read = read_csv(f, False) sub_dict[names[n]] = read return sub_dict