Advertisement
Uno-Dan

Untitled

Dec 9th, 2018
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. ########################################################################################################################
  2. #    File: main.py
  3. #  Author: Dan Huckson, https://github.com/unodan
  4. #    Date: 2018-12-09
  5. ########################################################################################################################
  6.  
  7. from json import load, dump
  8.  
  9. cities_source = 'cities.json'
  10. states_source = 'states.json'
  11. countries_source = 'countries.json'
  12.  
  13. cities = {"cities": []}
  14. states = {"states": []}
  15. countries = {"countries": []}
  16. countries_ids = (38, 230, 231)
  17.  
  18. cities_output = 'cities_file.json'
  19. states_output = 'states_file.json'
  20. countries_output = 'countries_file.json'
  21.  
  22.  
  23. with open(countries_source, 'r') as c:
  24.     data = load(c)
  25.     for country in data['countries']:
  26.         if int(country['id']) in countries_ids:
  27.             countries['countries'].append(country)
  28.  
  29.             with open(states_source, 'r') as s:
  30.                 data = load(s)
  31.                 for state in data['states']:
  32.                     if int(state['country_id']) == int(country['id']):
  33.                         states['states'].append(state)
  34.  
  35.                         with open(cities_source, 'r') as t:
  36.                             data = load(t)
  37.                             for city in data['cities']:
  38.                                 if int(city['state_id']) == int(state['id']):
  39.                                     cities['cities'].append(city)
  40.                                     print(country['name'], state['name'], city['name'])
  41.  
  42.     with open(countries_output, 'w') as f:
  43.         dump(countries, f, indent=4)
  44.  
  45.     with open(states_output, 'w') as f:
  46.         dump(states, f, indent=4)
  47.  
  48.     with open(cities_output, 'w') as f:
  49.         dump(cities, f, indent=4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement