forked from giggls/mapnik-german-l10n
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen_country_languages_table.py
executable file
·44 lines (38 loc) · 1.08 KB
/
gen_country_languages_table.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/python
# generate SQL Table from http://wiki.openstreetmap.org/wiki/Nominatim/Country_Codes
import sys
import urllib2
import re
content=urllib2.urlopen("http://wiki.openstreetmap.org/wiki/Nominatim/Country_Codes").read()
inside_table = False
col = 0
countries=[]
country={}
regex = re.compile("<.+?>", re.IGNORECASE)
for line in content.splitlines():
if '</table>' in line:
inside_table = False
if inside_table:
if '<td' in line:
line=regex.sub('',line).strip()
if col == 0:
country['iso']=line.lower()
if col == 1:
country['name']=line
if col == 3:
country['langs']=line.replace(", ",",")
# check for propper table alignment (<tr><td>)
if col == 0:
if '<tr>' not in oldline:
sys.stderr.write("invalid <tr><td>alignment")
sys.exit(1)
if col < 3:
col+=1
else:
countries.append(dict(country))
col=0
if line == '<table class="wikitable sortable">':
inside_table = True
oldline=line
for c in countries:
print "%s\t{%s}" % (c['iso'],c['langs'])