SHOW:
|
|
- or go back to the newest paste.
| 1 | #UPDATED TO FIX AN ERROR IN THE VOTER REGISTRATION CSV FILE | |
| 2 | - | # https://gofile.io/d/CmLAiD |
| 2 | + | |
| 3 | # https://gofile.io/d/NEpP4P | |
| 4 | ||
| 5 | ############################################## | |
| 6 | # Returns information about election results in MI based on county | |
| 7 | ||
| 8 | import csv | |
| 9 | ||
| 10 | ||
| 11 | office = 'President of the United States 4 Year Term (1) Position' | |
| 12 | congress = 'District Representative in Congress 2 Year Term (1) Position' | |
| 13 | counties = [] | |
| 14 | current_county = '' | |
| 15 | congress_county = '' | |
| 16 | results_dict = {}
| |
| 17 | with open('MI_results.csv',newline='') as csvfile:
| |
| 18 | res_reader = csv.DictReader(csvfile,delimiter=' ') | |
| 19 | ||
| 20 | for row in res_reader: | |
| 21 | if row['OfficeDescription'] == office: | |
| 22 | if current_county != row['CountyName']: | |
| 23 | #create the dict of the county with first candidate in csv and store it in the results dict | |
| 24 | results_dict[row['CountyName']] = {
| |
| 25 | row['CandidateLastName'] : row['CandidateVotes'] | |
| 26 | } | |
| 27 | counties.append(current_county) #create a list of counties | |
| 28 | current_county = row['CountyName'] #update the current county | |
| 29 | total_votes = int(row['CandidateVotes']) #start the county vote count | |
| 30 | else: | |
| 31 | #add candidate's votes to the county dict | |
| 32 | results_dict[row['CountyName']][row['CandidateLastName']] = row['CandidateVotes'] | |
| 33 | #add the total votes in the county | |
| 34 | total_votes += int(row['CandidateVotes']) | |
| 35 | #add total votes to the county dict (updates on last iteration to correct count) | |
| 36 | results_dict[row['CountyName']]['TotalVotes'] = total_votes | |
| 37 | elif congress in row['OfficeDescription']: | |
| 38 | if congress_county != row['CountyName']: | |
| 39 | congress_county = row['CountyName'] | |
| 40 | total_congress = int(row['CandidateVotes']) | |
| 41 | cong_key = row['PartyName']+'Congress' | |
| 42 | results_dict[congress_county][cong_key] = row['CandidateVotes'] | |
| 43 | else: | |
| 44 | cong_key = row['PartyName']+'Congress' | |
| 45 | results_dict[congress_county][cong_key] = row['CandidateVotes'] | |
| 46 | total_congress += int(row['CandidateVotes']) | |
| 47 | results_dict[row['CountyName']]['TotalCongress'] = total_congress | |
| 48 | ||
| 49 | ||
| 50 | ||
| 51 | # add the official number of registered voters to the dict | |
| 52 | with open('voters.csv',newline='') as csvfile:
| |
| 53 | v_reader = csv.DictReader(csvfile,delimiter=',') | |
| 54 | for row in v_reader: | |
| 55 | key = row['CountyName'] | |
| 56 | results_dict[key]['VotersRegistered'] = row['Voters'] | |
| 57 | turnout = int(results_dict[key]['TotalVotes'])/int(row['Voters']) | |
| 58 | results_dict[key]['Turnout'] = "{:.2%}".format(turnout)
| |
| 59 | ||
| 60 | #calculate percentages of votes per county and add to county dicts in results_dict | |
| 61 | with open('MI_results.csv',newline='') as csvfile:
| |
| 62 | res_reader = csv.DictReader(csvfile,delimiter=' ') | |
| 63 | for row2 in res_reader: | |
| 64 | if row2['OfficeDescription'] == office: | |
| 65 | key1 = row2['CountyName'] | |
| 66 | key2 = row2['CandidateLastName'] + 'Percent' | |
| 67 | candidate_percent = int(row2['CandidateVotes'])/int(results_dict[row2['CountyName']]['TotalVotes']) | |
| 68 | results_dict[key1][key2] = "{:.2%}".format(candidate_percent)
| |
| 69 | ||
| 70 | if congress in row2['OfficeDescription']: | |
| 71 | party_key = row2['PartyName']+'CongressPercent' | |
| 72 | party_percent = int(row2['CandidateVotes'])/int(results_dict[row2['CountyName']]['TotalCongress']) | |
| 73 | results_dict[row2['CountyName']][party_key] = "{:.2%}".format(party_percent)
| |
| 74 | ||
| 75 | ||
| 76 | for k, v in results_dict.items(): | |
| 77 | results_dict[k]['DEMDifference'] = int(results_dict[k]['Biden']) - int(results_dict[k]['DEMCongress']) | |
| 78 | results_dict[k]['REPDifference'] = int(results_dict[k]['Trump']) - int(results_dict[k]['REPCongress']) | |
| 79 | print('#########################################################################')
| |
| 80 | print('********* 2020 Michigan Presidental Election Results by County **********')
| |
| 81 | print('#########')
| |
| 82 | print('#########################################################################\n')
| |
| 83 | county_query = input('Enter a county: ')
| |
| 84 | county_query = county_query.upper() | |
| 85 | if county_query == 'ALL': | |
| 86 | print('\n Results for all Counties: \n')
| |
| 87 | for k, v in results_dict.items(): | |
| 88 | print(k.capitalize(), ' County: ') | |
| 89 | for k2, v2 in v.items(): | |
| 90 | print(k2, ': ', v2) | |
| 91 | else: | |
| 92 | print('\n Results for ', county_query, ' County: \n')
| |
| 93 | for k, v in results_dict[county_query].items(): | |
| 94 | if k == 'Trump' or k == 'TrumpPercent': | |
| 95 | print(k, ": ", v) | |
| 96 | elif k == 'Biden' or k == 'BidenPercent': | |
| 97 | print(k, ": ", v) | |
| 98 | elif k == 'REPCongress' or k == 'REPCongressPercent': | |
| 99 | print(k, ": ", v) | |
| 100 | elif k == 'DEMCongress' or k == 'DEMCongressPercent': | |
| 101 | print(k, ": ", v) | |
| 102 | elif k == 'REPDifference' or k == 'DEMDifference': | |
| 103 | print(k, ": ", v) | |
| 104 | elif k == 'DEMDifference' or k == 'DEMCongressPercent': | |
| 105 | print(k, ": ", v) | |
| 106 | ||
| 107 | print('Total Votes: ',results_dict[county_query]['TotalVotes'])
| |
| 108 | print('Total Congress Votes: ',results_dict[county_query]['TotalCongress'])
| |
| 109 | print('Registered Voters: ',results_dict[county_query]['VotersRegistered'])
| |
| 110 | print('Turnout: ', results_dict[county_query]['Turnout'])
| |
| 111 |