reformatter.py 473 B

1234567891011121314
  1. import csv
  2. from collections import defaultdict
  3. by_country = defaultdict(lambda: [])
  4. with open("worldcities.csv") as infile:
  5. reader = csv.reader(infile, delimiter=",", quotechar='"')
  6. next(reader) # skip header
  7. for _, name, lat, lng, _, iso2, *_ in reader:
  8. by_country[iso2].append((iso2.lower(), name, lat, lng))
  9. with open("urban-centers.csv", "w") as outfile:
  10. for k, v in by_country.items():
  11. csv.writer(outfile, delimiter=",", quotechar='"').writerows(v)