Guest User

Untitled

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