Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Code to find the least silver rates plan for a given zipcode
- import csv
- path = r'C:\Users\Doreen\Desktop\slcsp\slcsp.csv'
- path2 = r'C:\Users\Doreen\Desktop\slcsp\zips.csv'
- path3 = r'C:\Users\Doreen\Desktop\slcsp\plans.csv'
- # select all zipcodes from the slscp.csv file into a list called zipcodes.
- with open(path) as csv_file:
- csv_reader = csv.reader(csv_file, delimiter=',')
- next(csv_reader)
- zipcodes = [zipcode for zipcode, rate in csv_reader]
- # 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
- with open(path2) as zips_file:
- zips_reader = csv.reader(zips_file, delimiter=',')
- next(zips_reader)
- file = [([z, i[1], i[4]]) for i in zips_reader for z in zipcodes if i[0] == z]
- res3_list = []
- res4_list = []
- res5_list = []
- # remove all duplicate tuples containing the zipcode, county name and rate area from the file list
- res_list = [file[i] for i in range(len(file)) if file[i] not in file[i + 1:]]
- # to create a list containing only the zipcodes from the filtered_file
- res1_list = [i[0] for i in res_list]
- # remove all duplicate zipcodes in the zipcodes_file
- res2_list = [n for x, n in enumerate(res1_list) if n not in res1_list[:x]]
- # loop through the filtered zipcodes through the list containing filtered tuples containing the zipcode, county name and rate area
- # if the first index in the filtered zipcodes list is found in the zipcodes file and the number of times in
- for i in res_list:
- if i[0] in res1_list and res1_list.count(i[0]) == 1:
- res3_list.append([i[0], i[1], i[2]])
- elif i[0] in res1_list and res1_list.count(i[0]) != 1:
- res4_list.append(i[0])
- # remove all duplicate zipcodes in the error_list
- res5_list = [n for x, n in enumerate(res4_list) if n not in res4_list[:x]]
- # loops through the plans.csv file and to check if specific values from res3_list matches with those from the file.
- with open(path3) as plans_file:
- plans_reader = csv.reader(plans_file, delimiter=',')
- next(plans_reader)
- for a in plans_reader:
- for b in res3_list:
- if a[1] == b[1] and a[4] == b[2] and a[2] == 'Silver':
- b.append(float(a[3]))
- # 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
- # when the list has been sorted, the rates are deleted and the sorted list are appendend in its place
- for i in res3_list:
- sort_list = i[3:]
- if sort_list != []:
- sort_list.sort(reverse=True)
- del i[3:]
- i.append(sort_list)
- else:
- sort_list = ''
- # loops through the list containing tuples of the zipcodes, county name and sorted list and then selects the second index from the sorted list
- # it deletes the sorted lists and replace them with the selected index
- for i in res3_list:
- if len(i[-1]) != 1:
- i.append(i[-1][1])
- del i[-2]
- # loops through the res3_list and then appends the zipcode and the selected index from the code above
- #
- final = []
- for i in res3_list:
- try:
- final.append([i[0], i[3]])
- except IndexError:
- final.append([i[0], ''])
- # appends the list containing zipcodes with the filtered duplicate zipcodes and append them with the corresponding rate as empty
- for i in res5_list:
- final.append([i, ''])
- # rewriting the slscp.csv file with the final results
- with open('slscp.csv', 'w', newline='') as slscp_file:
- slscp_writer = csv.writer(slscp_file, delimiter=',')
- slscp_writer.writerow(['zipcode', 'rate area'])
- for i in final:
- slscp_writer.writerow(i)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement