Skip to content

Instantly share code, notes, and snippets.

@fwenzel
Created September 3, 2015 18:43
Show Gist options
  • Save fwenzel/8d879779ef608908b804 to your computer and use it in GitHub Desktop.
Save fwenzel/8d879779ef608908b804 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import json
import re
from xml.etree import ElementTree as ET
NS = {'genc': 'http://api.nsgreg.nga.mil/schema/genc/2.0'}
ent = lambda name, ns: '{%s}%s' % (NS[ns], name)
tree = ET.parse('genc.xml')
root = tree.getroot()
countries = {}
for entity in root:
if entity.tag != ent('GeopoliticalEntity', 'genc'):
continue
code_node = entity.find('.//%s' % ent('char2Code', 'genc'))
name_node = entity.find(ent('shortName', 'genc'))
if code_node is None or name_node is None:
continue
code = code_node.text.lower()
name = name_node.text
# Omit US gov't entity codes.
if re.match(r'a\d', code):
continue
countries[code] = name
print json.dumps(countries, indent=4, sort_keys=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment