Created
March 22, 2019 14:28
-
-
Save symroe/ac7dafdaf3d00b9cbee343cf91e7bbcc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import csv | |
import sys | |
populaton_url = "https://petitionmap.unboxedconsulting.com/json/mps/population_ons.json" | |
req = requests.get(populaton_url) | |
POULATION_BY_GSS = req.json() | |
pet_url = "https://petition.parliament.uk/petitions/241584.json" | |
req = requests.get(pet_url) | |
PET_DATA = req.json() | |
BY_CONSTITUENCY = PET_DATA['data']['attributes']['signatures_by_constituency'] | |
fieldnames = ["name", "ons_code", "mp", "signature_count", "population", "signed_percent"] | |
out_file = csv.DictWriter(sys.stdout, fieldnames=fieldnames, delimiter="\t") | |
for constituency in BY_CONSTITUENCY: | |
constituency['population'] = POULATION_BY_GSS[constituency["ons_code"]]['population'] | |
constituency['signed_percent'] = round((constituency['signature_count'] / constituency['population'] ) * 100, 2) | |
out_file.writerow(constituency) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment