Advertisement
Uno-Dan

Untitled

Dec 15th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.12 KB | None | 0 0
  1. ########################################################################################################################
  2. #    File: main.py
  3. #  Author: Dan Huckson, https://github.com/unodan
  4. #    Date: 2018-12-15
  5. ########################################################################################################################
  6.  
  7. import csv
  8. import os
  9.  
  10. countries_source = '2018-1 SubdivisionCodes.csv'
  11.  
  12. cities = []
  13. countries = []
  14.  
  15. COUNTRIES = ['CA', 'US']
  16. encoding = 'ISO 8859-1'
  17.  
  18. files = []
  19. for file in os.listdir('.'):
  20.     if 'UNLOCODE' in file:
  21.         files.append(file)
  22. files = sorted(files)
  23.  
  24. with open(countries_source, encoding=encoding) as f:
  25.     reader = csv.reader(f, delimiter=',', quotechar='"')
  26.  
  27.     for row in reader:
  28.         country_code, zone_code, zone_name, zone_type = row
  29.         if country_code in COUNTRIES:
  30.             countries.append({
  31.                 'country_code': country_code,
  32.                 'zone_code': zone_code,
  33.                 'zone_name': zone_name,
  34.                 'zone_type': zone_type,
  35.             })
  36.  
  37.     for country in countries:
  38.         file = None
  39.         found = False
  40.         for file in files:
  41.             with open(file, encoding=encoding) as g:
  42.                 data = g.readlines()
  43.  
  44.             end_country_code = data[-1].split(',')[1].strip('"')
  45.             if country['country_code'] <= end_country_code:
  46.                 found = True
  47.                 break
  48.  
  49.         with open(file, encoding=encoding) as g:
  50.             reader = csv.reader(g, delimiter=',', quotechar='"')
  51.  
  52.             cnt = 0
  53.             for row in reader:
  54.                 if country['country_code'] == row[1]:
  55.                     country_code = row[1]
  56.                     city = row[3]
  57.                     zone_code = row[5]
  58.                     if country_code in COUNTRIES:
  59.                         cities.append({
  60.                             'country_code': country_code,
  61.                             'zone_code': zone_code,
  62.                             'zone_type': 3,
  63.                             'city': city,
  64.  
  65.                         })
  66.     for city in cities:
  67.         print(city)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement