import json
import pandas as pd
def find_segment(province_queried):
with open('research.geojson', encoding='utf-8-sig') as f:
dct_research = json.load(f)
for feature in dct_research['feature']:
for key in feature.get("properties", {}).get("results", {}):
province = feature.get("properties", {}).get("name")
segments = feature.get("properties", {}).get("segments")
if province == province_queried:
return segments
def main():
with open('constituencies.json') as f:
dct_constituencies = json.load(f)
for feature in dct_constituencies['features']:
for key in feature.get("properties", {}).get("results", {}):
province = feature.get("properties", {}).get("name_1")
constituency = feature.get("properties", {}).get("name_4", {})
segments = find_segment(province)
d.append({"Party Affiliation": key,
"Province": province,
"Constituency Name": constituency,
"segments": segments})
column_names = ["Province", "Constituency Name", "Party Affiliation", "segments"]
df = pd.DataFrame(d, columns=column_names)
df.to_csv("constituencies_with_segments.csv")
if __name__ == '__main__':
main()