generate-countries-table.py 941 B

12345678910111213141516171819202122232425262728293031323334
  1. #!/usr/bin/env python3
  2. # usage: generate-countries-table.py > countries.csv
  3. # in sqlite3 terminal:
  4. #
  5. # .mode csv
  6. # .headers off
  7. # .separator ";"
  8. # .import 'full path to countries.csv' 'v(database_version)_countries'
  9. #
  10. import csv
  11. import os
  12. import sys
  13. TSV_FILE = '../data/country_names.tsv'
  14. # Use another name in the country_code header if you want countries names in different language
  15. READABLE_NAME = 'en'
  16. # ord("🇦") - ord("A")
  17. FLAG_OFFSET = 127397
  18. if not os.path.exists(TSV_FILE):
  19. print("File = {} does not exist.".format(TSV_FILE))
  20. sys.exit(1)
  21. with open(TSV_FILE, 'r') as tsvfile:
  22. country = csv.DictReader(tsvfile, delimiter='\t', quotechar='"')
  23. # Skip header
  24. next(country)
  25. for row in country:
  26. country_code = row['country_code']
  27. codepoints = [ord(x) + FLAG_OFFSET for x in country_code]
  28. print('%s;%s;%s' % (country_code, chr(codepoints[0]) + chr(codepoints[1]), row[READABLE_NAME]))