We_Rise

Edison Script

Nov 10th, 2020
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import json
  2. import glob, os
  3.  
  4. csv_content="state,timestamp,votes,eevp,trumpd,bidenj\r\n"
  5.  
  6. #read json files
  7. for file in glob.glob("*.json"):
  8. with open(file, encoding="utf8") as f:
  9. x = json.load(f)
  10. xts = x["data"]["races"][0]["timeseries"]
  11. for i in range(len(xts)):
  12. csv_content=csv_content+f'{file[:-5]},{xts[i]["timestamp"]},{xts[i]["votes"]},{xts[i]["eevp"]},{xts[i]["vote_shares"]["trumpd"]},{xts[i]["vote_shares"]["bidenj"]}\r\n'
  13.  
  14. #write csv table
  15. with open('result.csv', 'w', newline='') as f:
  16. f.write(csv_content)
  17.  
Add Comment
Please, Sign In to add comment