Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. # Code to find the least silver rates plan for a given zipcode
  2. import csv
  3. path = r'C:\Users\Doreen\Desktop\slcsp\slcsp.csv'
  4. path2 = r'C:\Users\Doreen\Desktop\slcsp\zips.csv'
  5. path3 = r'C:\Users\Doreen\Desktop\slcsp\plans.csv'
  6.  
  7. # select all zipcodes from the slscp.csv file into a list called zipcodes.
  8. with open(path) as csv_file:
  9. csv_reader = csv.reader(csv_file, delimiter=',')
  10. next(csv_reader)
  11. zipcodes = [zipcode for zipcode, rate in csv_reader]
  12.  
  13. # get the county name and rate area for a particular zipcode by looping the zipcodes file containing all the zipcodes extracted from the slscp file with zips file
  14. with open(path2) as zips_file:
  15. zips_reader = csv.reader(zips_file, delimiter=',')
  16. next(zips_reader)
  17. file = [([z, i[1], i[4]]) for i in zips_reader for z in zipcodes if i[0] == z]
  18.  
  19. res3_list = []
  20. res4_list = []
  21. res5_list = []
  22.  
  23. # remove all duplicate tuples containing the zipcode, county name and rate area from the file list
  24. res_list = [file[i] for i in range(len(file)) if file[i] not in file[i + 1:]]
  25.  
  26. # to create a list containing only the zipcodes from the filtered_file
  27. res1_list = [i[0] for i in res_list]
  28.  
  29. # remove all duplicate zipcodes in the zipcodes_file
  30. res2_list = [n for x, n in enumerate(res1_list) if n not in res1_list[:x]]
  31.  
  32. # loop through the filtered zipcodes through the list containing filtered tuples containing the zipcode, county name and rate area
  33. # if the first index in the filtered zipcodes list is found in the zipcodes file and the number of times in
  34. for i in res_list:
  35. if i[0] in res1_list and res1_list.count(i[0]) == 1:
  36. res3_list.append([i[0], i[1], i[2]])
  37. elif i[0] in res1_list and res1_list.count(i[0]) != 1:
  38. res4_list.append(i[0])
  39.  
  40. # remove all duplicate zipcodes in the error_list
  41. res5_list = [n for x, n in enumerate(res4_list) if n not in res4_list[:x]]
  42.  
  43. # loops through the plans.csv file and to check if specific values from res3_list matches with those from the file.
  44. with open(path3) as plans_file:
  45. plans_reader = csv.reader(plans_file, delimiter=',')
  46. next(plans_reader)
  47. for a in plans_reader:
  48. for b in res3_list:
  49. if a[1] == b[1] and a[4] == b[2] and a[2] == 'Silver':
  50. b.append(float(a[3]))
  51.  
  52. # sorts through the res3_list to get all the rates appended to the list from the above code assigns it to another list and then sort them in descending order
  53. # when the list has been sorted, the rates are deleted and the sorted list are appendend in its place
  54. for i in res3_list:
  55. sort_list = i[3:]
  56. if sort_list != []:
  57. sort_list.sort(reverse=True)
  58. del i[3:]
  59. i.append(sort_list)
  60. else:
  61. sort_list = ''
  62. # loops through the list containing tuples of the zipcodes, county name and sorted list and then selects the second index from the sorted list
  63. # it deletes the sorted lists and replace them with the selected index
  64. for i in res3_list:
  65. if len(i[-1]) != 1:
  66. i.append(i[-1][1])
  67. del i[-2]
  68.  
  69. # loops through the res3_list and then appends the zipcode and the selected index from the code above
  70. #
  71. final = []
  72. for i in res3_list:
  73. try:
  74. final.append([i[0], i[3]])
  75. except IndexError:
  76. final.append([i[0], ''])
  77.  
  78. # appends the list containing zipcodes with the filtered duplicate zipcodes and append them with the corresponding rate as empty
  79. for i in res5_list:
  80. final.append([i, ''])
  81.  
  82. # rewriting the slscp.csv file with the final results
  83. with open('slscp.csv', 'w', newline='') as slscp_file:
  84. slscp_writer = csv.writer(slscp_file, delimiter=',')
  85. slscp_writer.writerow(['zipcode', 'rate area'])
  86. for i in final:
  87. slscp_writer.writerow(i)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement