Advertisement
Guest User

Untitled

a guest
Nov 11th, 2020
998
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. import json
  2.  
  3. def loadData():
  4. with open('minnesota_data.json', encoding='utf-8') as f:
  5. data = json.load(f)
  6. return data["data"]["races"][0]["timeseries"]
  7.  
  8. def generateVoteCounts(data):
  9. generated_data = []
  10. # first things first, sort by timestamp
  11. data.sort(key=lambda x: x['timestamp'])
  12. trump = 0
  13. biden = 0
  14. other = 0
  15. total_votes = 0
  16. for result in data:
  17. # NOTE: there is one bad data entry at timestamp: 2020-11-04T09:55:44Z we skip this one because it has no data in it.
  18. if result['timestamp'] == '2020-11-04T09:55:44Z':
  19. print ('skipping the bad entry at 2020-11-04T09:55:44Z')
  20. continue
  21. b_shares = result['vote_shares']['bidenj']
  22. t_shares = result['vote_shares']['trumpd']
  23. o_shares = 1.0 - b_shares - t_shares
  24. total = result['votes']
  25. t_count = t_shares * total
  26. b_count = b_shares * total
  27. o_count = total - t_count - b_count
  28. t_change = t_count - trump
  29. b_change = b_count - biden
  30. o_change = o_count - other
  31. try:
  32. t_per = t_change / trump
  33. except:
  34. t_per = 0.0
  35. try:
  36. b_per = b_change / biden
  37. except:
  38. b_per = 0.0
  39. try:
  40. o_per = o_change / other
  41. except:
  42. o_per = 0.0
  43. total_change = total - total_votes
  44. total_votes = total
  45. trump = t_count
  46. biden = b_count
  47. other = o_count
  48. result['calc'] = {
  49. 'other_shares': o_shares,
  50. 'trump_count': t_count,
  51. 'biden_count': b_count,
  52. 'other_count': o_count,
  53. 'trump_change': t_change,
  54. 'biden_change': b_change,
  55. 'other_change': o_change,
  56. 'total_change': total_change,
  57. 'trump_p_change': t_per,
  58. 'biden_p_change': b_per,
  59. 'other_p_change': o_per
  60. }
  61. generated_data.append(result)
  62. return generated_data
  63.  
  64. def saveToCSV(data):
  65. # gotta format it first
  66. first_line = 'timestamp,total votes,trump %,biden %,3rd party %,trump votes,biden votes,3rd party votes,3rd party change,3rd party % change,trump change,trump % change, biden change,biden % change,total change'+'\n'
  67. with open('result_all.csv', 'w', newline='') as f:
  68. f.write(first_line)
  69. for result in data:
  70. line_array = []
  71. line_array.append(str(result['timestamp']))
  72. line_array.append(str(result['votes']))
  73. line_array.append(str(result['vote_shares']['trumpd']))
  74. line_array.append(str(result['vote_shares']['bidenj']))
  75. line_array.append(str(result['calc']['other_shares']))
  76. line_array.append(str(result['calc']['trump_count']))
  77. line_array.append(str(result['calc']['biden_count']))
  78. line_array.append(str(result['calc']['other_count']))
  79. line_array.append(str(result['calc']['other_change']))
  80. line_array.append(str(result['calc']['other_p_change']))
  81. line_array.append(str(result['calc']['trump_change']))
  82. line_array.append(str(result['calc']['trump_p_change']))
  83. line_array.append(str(result['calc']['biden_change']))
  84. line_array.append(str(result['calc']['biden_p_change']))
  85. line_array.append(str(result['calc']['total_change']))
  86. line = ','.join(line_array) + '\n'
  87. f.write(line)
  88.  
  89.  
  90. if __name__ == '__main__':
  91. results = loadData()
  92. results = generateVoteCounts(results)
  93. saveToCSV(results)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement