Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ########################################################################################################################
- # File: main.py
- # Author: Dan Huckson, https://github.com/unodan
- # Date: 2018-12-15
- ########################################################################################################################
- import csv
- import os
- countries_source = '2018-1 SubdivisionCodes.csv'
- cities = []
- countries = []
- COUNTRIES = ['CA', 'US']
- encoding = 'ISO 8859-1'
- files = []
- for file in os.listdir('.'):
- if 'UNLOCODE' in file:
- files.append(file)
- files = sorted(files)
- with open(countries_source, encoding=encoding) as f:
- reader = csv.reader(f, delimiter=',', quotechar='"')
- for row in reader:
- country_code, zone_code, zone_name, zone_type = row
- if country_code in COUNTRIES:
- countries.append({
- 'country_code': country_code,
- 'zone_code': zone_code,
- 'zone_name': zone_name,
- 'zone_type': zone_type,
- })
- for country in countries:
- file = None
- found = False
- for file in files:
- with open(file, encoding=encoding) as g:
- data = g.readlines()
- end_country_code = data[-1].split(',')[1].strip('"')
- if country['country_code'] <= end_country_code:
- found = True
- break
- with open(file, encoding=encoding) as g:
- reader = csv.reader(g, delimiter=',', quotechar='"')
- cnt = 0
- for row in reader:
- if country['country_code'] == row[1]:
- country_code = row[1]
- city = row[3]
- zone_code = row[5]
- if country_code in COUNTRIES:
- cities.append({
- 'country_code': country_code,
- 'zone_code': zone_code,
- 'zone_type': 3,
- 'city': city,
- })
- for city in cities:
- print(city)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement