Created
September 3, 2015 18:43
-
-
Save fwenzel/8d879779ef608908b804 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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