3
# Read iso-codes data file and output a .tab file
5
# Copyright (C) 2004 Alastair McKinstry <mckinstry@debian.org>
6
# Released under the GPL.
7
# $Id: iso3166tab.py,v 1.1 2004/05/20 21:24:39 mckinstry Exp $
9
from xml.sax import saxutils, make_parser, saxlib, saxexts, ContentHandler
10
from xml.sax.handler import feature_namespaces
11
import sys, os, getopt, urllib2
13
class printLines(saxutils.DefaultHandler):
14
def __init__(self, ofile):
17
def startElement(self, name, attrs):
18
if name != 'iso_3166_entry':
20
code = attrs.get('alpha_2_code', None)
22
raise RunTimeError, "Bad file"
23
if type(code) == unicode:
24
code = code.encode('UTF-8')
25
name = attrs.get('name', None)
27
raise RunTimeError, " BadFile"
28
if type(name) == unicode:
29
name = name.encode('UTF-8')
30
common_name = attrs.get('common_name', None)
31
if common_name != None:
32
if type(common_name) == unicode:
33
name = common_name.encode('UTF-8')
36
self.ofile.write (code + '\t' + name + '\n')
46
p.setErrorHandler(saxutils.ErrorPrinter())
48
dh = printLines(ofile)
49
p.setContentHandler(dh)
50
p.parse('iso_3166.xml')
52
print in_sysID+": "+str(e)
53
except saxlib.SAXException,e: