# Sorting Desired Excel Data Quickly # print("This program takes a list of data from the CDC spreadsheet and anaylses it by finding the states that had the minimum and maximum number") print("in each of the selected categories. Which in this program the selected categories consisted of Heart Disease Death Rate, Motor Vehicle Death Rate,") print("Teen Birth Rate, Adult Smoking, and Adult Obesity.") print("="*150) openedFile = False while (not openedFile): #Openfile is false so runs this program until true fileName = input("Open what file: ") #Input the filename to open *Use riskfactors.csv* output_file = open("best_and_worst.txt",'w') #Opens an empty output file to write the desired information try: file = open(fileName,'r') #Try's to open the file that was inputted in by user openedFile = True #If that file name exist the openedFile is true and can exit the loop except IOError: #When there is an IOError then that file doesn't exist so makes program print... print("Bad file name, try again ") #This comment when the file doesn't exist. Then loop restarts. for i in range (6): #This gets rid of first few lines in code because it is useless information file.readline() L = [] #Empty List L1 = [] #Empty List L2 = [] #Empty List L3 = [] #Empty List L4 = [] #Empty List L5 = [] #Empty List def appender(x,y,z,line_list): #def function to make list without % sign x.append(float(line_list[z])) #Makes list of needed number x.append(line_list[0]) #Appends the corresponding state to list y.append(x) #Makes another list of number and state together def percentage_appender(x,y,z,line_list): #def function to make list with % sign x.append(line_list[z]) #Makes list of needed number x.append(line_list[0]) #Appends the corresponding state to list y.extend(x) #Makes another list of number and state together def run_program(L6): #Def function to run the program to get the needed information for line in file: #Goes through every line in the file L6 = [] #Empty List L7 = [] #Empty List L8 = [] #Empty List L9 = [] #Empty List L10 = [] #Empty List line = line.strip() #Strips each line into a list line_list = line.split(',') #Seperates each word by a comma making each item its seperate string L.append(line_list[0]) #Def function that appends the state appender(L6,L1,1,line_list) #Def function that appends selected number with corresponding state appender(L7,L2,5,line_list) #Def function that appends selected number with corresponding state appender(L8,L3,7,line_list) #Def function that appends selected number with corresponding state percentage_appender(L9,L4,11,line_list) #Def function that appends selected percent with corresponding state percentage_appender(L10,L5,13,line_list)#Def function that appends selected percent with corresponding state L1_min = [] # flips the position of state and number in the list L1_min.extend(min(L1)) # L1_min.reverse() # # L1_max = [] # L1_max.extend(max(L1)) # L1_max.reverse() # # L2_min = [] # L2_min.extend(min(L2)) # L2_min.reverse() # # L2_max = [] # L2_max.extend(max(L2)) # L2_max.reverse() # # L3_min = [] # L3_min.extend(min(L3)) # L3_min.reverse() # # L3_max = [] # L3_max.extend(max(L3)) # L3_max.reverse() # # L4_min = [] # L4_min.extend(min(L4)) # L4_min.reverse() # # L4_max = [] # L4_max.extend(max(L4)) # L4_max.reverse() # # L5_min = [] # L5_min.extend(min(L5)) # L5_min.reverse() # # L5_max = [] # L5_max.extend(max(L5)) # L5_max.reverse() #All of the list are flipped around now for row in (L4): #Puts L4 into a column t = [] #Empty List for i in range(0,102,2): #Makes a range of i going by 2 starting at 0 up to 102 t.extend([L4[i][0:-1:1]]) #Uses the i to select all the string numbers in L4 t = [float(i) for i in t] #Makes a new list of the string numbers into float numbers h = [] #Empty List for i in range(1,102,2): #Makes a range of i going by 2 starting at 1 up to 102 h.append([L4[i]]) #Uses the i to select all the states in L4 X = [] #Empty List for i in range(0,51,1): #Makes a range of i going by 1 starting at 0 up to 51 X.append([t[i],h[i]]) #Combines to list making the float number attached to the corresponding state X_min = [] #Flips the state and float number around and eliminates extra brackets # X_min.extend(min(X)) # X_min.reverse() # # X_max = [] # X_max.extend(max(X)) # X_max.reverse() # for row in (L5): #Does that exact same thing as descriped about except for L5 now. z = [] # for i in range(0,102,2): # z.extend([L5[i][0:-1:1]]) # z = [float(i) for i in z] # h = [] # for i in range(1,102,2): # h.append([L5[i]]) # Y = [] # for i in range(0,51,1): # Y.append([z[i],h[i]]) # # Y_min = [] # Y_min.extend(min(Y)) # Y_min.reverse() # # Y_max = [] # Y_max.extend(max(Y)) # Y_max.reverse() #All the numbers are now converted into min and max print("="*35) print(L1,file = output_file) print('{:20s} : {:<36} {:11s}'.format("Indicator","Min","Max")) print('Heart Disease Death Rate (2007): {:12s} {:15.1f} {:10s} {:10.1f}'.format(L1_min[0],L1_min[1],L1_max[0],L1_max[1])) print('Motor Vehicle Death Rate(2009) : {:12s} {:6.1f} {:10s} {:11.1f}'.format(L2_min[0],L2_min[1],L2_max[0],L2_max[1])) print('Teen Birth Rate (2009) : {:12s} {:11.1f} {:10s} {:10.1f}'.format(L3_min[0],L3_min[1],L3_max[0],L3_max[1])) print('Adult Smoking (2010) : {:12s} {:12.1f} {:10s} {:8.1f}'.format(X_min[0][0],X_min[1],X_max[0][0],(X_max[1]))) print('Adult Obesity (2010) : {:12s} {:12.1f} {:10s} {:10.1f}'.format(Y_min[0][0],Y_min[1],Y_max[0][0],(Y_max[1]))) run_program([]) #def program that runs everything file.close() #Closes the reading file output_file.close() #Closes the writing file