Last active
April 21, 2020 18:19
-
-
Save simonpojok/9924cd423d318060d9c804693eb72b91 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import argparse | |
from collections import OrderedDict | |
def makeList(file=None, isSilver=False): | |
data = [] | |
with open(file, 'r') as opened_file: | |
reader = csv.DictReader(opened_file) | |
if isSilver: | |
for row in reader: | |
if row['metal_level'] == 'Silver': | |
data.append(row) | |
else: | |
for row in reader: | |
data.append(row) | |
next(reader, None) | |
return data | |
def saveToCSV(file_path=None, data_dict={}): | |
with open(file_path, 'w') as f: | |
writer = csv.DictWriter(f, ['zipcode', 'rate'], lineterminator='\n') | |
writer.writeheader() | |
for zipcode, rates in data_dict.items(): | |
if len(rates) > 1: | |
rates.sort() | |
writer.writerow({'zipcode': zipcode, 'rate': rates[1]}) | |
else: | |
writer.writerow({'zipcode': zipcode, 'rate': str(rates)}) | |
parser = argparse.ArgumentParser(description='some data') | |
parser.add_argument('-s', '--slcsp', help='CSV file c', required=True) | |
parser.add_argument('-p', '--plans', help='CSV with plans', required=True) | |
parser.add_argument('-z', '--zips', help='zip codes', required=True) | |
args = vars(parser.parse_args()) | |
slcsp = makeList(args['slcsp'], False) | |
plans = makeList(args['plans'], True) | |
zips = makeList(args['zips'], False) | |
zipcode_dict_state_rate_area = OrderedDict() | |
for zipcode_from_slcsp in slcsp: | |
that_zipcode_from_slcsp = zipcode_from_slcsp['zipcode'] | |
for zipcode_from_zips in zips: | |
if that_zipcode_from_slcsp == zipcode_from_zips['zipcode']: | |
if that_zipcode_from_slcsp in zipcode_dict_state_rate_area: | |
if zipcode_dict_state_rate_area[that_zipcode_from_slcsp]['rate_area'] != zipcode_from_zips['rate_area']: | |
zipcode_dict_state_rate_area[that_zipcode_from_slcsp] = None | |
break | |
else: | |
zipcode_dict_state_rate_area[that_zipcode_from_slcsp] = { | |
'rate_area':zipcode_from_zips['rate_area'], | |
'state': zipcode_from_zips['state'] | |
} | |
#print(zipcode_dict_state_rate_area) | |
silver_plans = [ | |
#empty now | |
] | |
answers = OrderedDict( | |
#empty now | |
) | |
for zipcode, state_rate_area in zipcode_dict_state_rate_area.items(): | |
if not zipcode_dict_state_rate_area[zipcode]: # zipcode has not rate_area and state data | |
answers[zipcode] = '' | |
else: | |
for silver_plan in plans: | |
if state_rate_area['rate_area'] == silver_plan['rate_area'] and state_rate_area['state'] == silver_plan['state']: | |
if zipcode in answers: | |
answers[zipcode].append(silver_plan['rate']) | |
else: | |
answers[zipcode] = [silver_plan['rate']] #something here messy | |
if zipcode not in answers: | |
answers[zipcode] = '' | |
saveToCSV('row.csv', answers) | |
def printStdout(file=None, mode='r'): | |
print('.......................| Writing to stdout |.............................') | |
print(' ' + 'zipcode, rate') | |
with open( file, mode) as openedfile: | |
reader = csv.DictReader(openedfile) | |
for row in reader: | |
print(' ' + str(row['zipcode']) + ' , ' + str(row['rate'])) | |
printStdout(file='row.csv') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To run the program, please do this
python slcsp.py -s=slcsp.csv -p=plans.csv -z=zips.csv
or
python --slcsp=slcsp.csv --plans=plans.csv --zips=zips.csv
the hit enter