PYTHON   7

prepare_countries_names.py

Guest on 3rd June 2021 05:41:29 AM

  1. import json
  2. import copy
  3.  
  4. codes = json.load(open('./data/country_codes.json'))
  5. countries = json.load(open('./data/ne_countries.geojson'))
  6.  
  7. ids_to_remove = []
  8.  
  9. for ci, country in enumerate(countries['features']):
  10.     found = False
  11.     for code in codes:
  12.         if code['Code'] == country['properties']['ISO_A2']:
  13.             found = True
  14.             country['properties']['admin'] = code['Name']
  15.  
  16.     if found == False:
  17.         country['properties']['admin'] = country['properties']['NAME']
  18.         #ids_to_remove.append(ci)
  19.  
  20. #ids_to_remove.reverse()
  21.  
  22. #for id in ids_to_remove:
  23. #del countries['features'][id]
  24.  
  25. with open('./data/countries_new.geojson', 'w') as file:
  26.     json.dump(countries, file)

Raw Paste


Login or Register to edit or fork this paste. It's free.