Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- def loadData():
- with open('minnesota_data.json', encoding='utf-8') as f:
- data = json.load(f)
- return data["data"]["races"][0]["timeseries"]
- def generateVoteCounts(data):
- generated_data = []
- # first things first, sort by timestamp
- data.sort(key=lambda x: x['timestamp'])
- trump = 0
- biden = 0
- other = 0
- total_votes = 0
- for result in data:
- # 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.
- if result['timestamp'] == '2020-11-04T09:55:44Z':
- print ('skipping the bad entry at 2020-11-04T09:55:44Z')
- continue
- b_shares = result['vote_shares']['bidenj']
- t_shares = result['vote_shares']['trumpd']
- o_shares = 1.0 - b_shares - t_shares
- total = result['votes']
- t_count = t_shares * total
- b_count = b_shares * total
- o_count = total - t_count - b_count
- t_change = t_count - trump
- b_change = b_count - biden
- o_change = o_count - other
- try:
- t_per = t_change / trump
- except:
- t_per = 0.0
- try:
- b_per = b_change / biden
- except:
- b_per = 0.0
- try:
- o_per = o_change / other
- except:
- o_per = 0.0
- total_change = total - total_votes
- total_votes = total
- trump = t_count
- biden = b_count
- other = o_count
- result['calc'] = {
- 'other_shares': o_shares,
- 'trump_count': t_count,
- 'biden_count': b_count,
- 'other_count': o_count,
- 'trump_change': t_change,
- 'biden_change': b_change,
- 'other_change': o_change,
- 'total_change': total_change,
- 'trump_p_change': t_per,
- 'biden_p_change': b_per,
- 'other_p_change': o_per
- }
- generated_data.append(result)
- return generated_data
- def saveToCSV(data):
- # gotta format it first
- 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'
- with open('result_all.csv', 'w', newline='') as f:
- f.write(first_line)
- for result in data:
- line_array = []
- line_array.append(str(result['timestamp']))
- line_array.append(str(result['votes']))
- line_array.append(str(result['vote_shares']['trumpd']))
- line_array.append(str(result['vote_shares']['bidenj']))
- line_array.append(str(result['calc']['other_shares']))
- line_array.append(str(result['calc']['trump_count']))
- line_array.append(str(result['calc']['biden_count']))
- line_array.append(str(result['calc']['other_count']))
- line_array.append(str(result['calc']['other_change']))
- line_array.append(str(result['calc']['other_p_change']))
- line_array.append(str(result['calc']['trump_change']))
- line_array.append(str(result['calc']['trump_p_change']))
- line_array.append(str(result['calc']['biden_change']))
- line_array.append(str(result['calc']['biden_p_change']))
- line_array.append(str(result['calc']['total_change']))
- line = ','.join(line_array) + '\n'
- f.write(line)
- if __name__ == '__main__':
- results = loadData()
- results = generateVoteCounts(results)
- saveToCSV(results)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement