""" lab.14 """ #input: nothing #output: prints each state and number of customers per state #description: go through customer data file one line at a time # and generate state distributions. def getStateDistributions(): customerFile = open("FakeCustomerData.txt","r") #make an empty dictionary stateDict = {} #skip the header header = customerFile.readline() #go through each line for line in customerFile: #split into a list lineList = line.split(",") state = lineList[7] # add to dictionary # if not in dictionary if state not in stateDict: stateDict[state] = 1 # else is in dictionary else: stateDict[state] += 1 #If we printed out the dictionary here, items would print #out of order. #for key in stateDict: # print(key, "->", stateDict[key]) #Sort the keys by placing them in a sorted keys list. keyList = sorted(stateDict) for key in keyList: print(key, "->", stateDict[key]) customerFile.close() #input: csv file to open and a column number to process #output: prints each state and number of customers per stat at columnNum #description: go through customer data file one line at a time # and generate columnNum distributions. def getColumnDistribution(filename, columnNum): customerFile = open(filename,"r") #make an empty dictionary statDict = {} #use the header to figure out if columnNum is too big header = customerFile.readline() headerList = header.split(",") if columnNum >= len(headerList): print("There is no column number", columnNum) return None #go through each line for line in customerFile: #split into a list lineList = line.split(",") stat = lineList[columnNum] # add to dictionary # if not in dictionary if stat not in statDict: statDict[stat] = 1 # else is in dictionary else: statDict[stat] += 1 #Sort the keys by placing them in a sorted keys list. keyList = sorted(statDict) for key in keyList: print(key, "->", statDict[key]) customerFile.close() #input: nothing #output: prints each birth year and number of customers per birth year #description: go through customer data file one line at a time # and generate birth year distributions. def getBirthYearDistributions(): customerFile = open("FakeCustomerData.txt","r") #make an empty dictionary birthDict = {} #skip the header header = customerFile.readline() #go through each line for line in customerFile: #split into a list lineList = line.split(",") birthday = lineList[13] #Now get only the year bdayList = birthday.split("/") year = bdayList[2] # add to dictionary # if not in dictionary if year not in birthDict: birthDict[year] = 1 # else is in dictionary else: birthDict[year] += 1 #Sort the keys by placing them in a sorted keys list. keyList = sorted(birthDict) for key in keyList: print(key, "->", birthDict[key]) customerFile.close()