Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import xml.parsers.expat
- import urllib
- url = 'http://api.erepublik.com/v2/feeds/countries'
- f = urllib.urlopen(url)
- s = ''
- for line in f:
- s += line
- f.close()
- population = ''
- checkPop = False
- checkName = False
- inRegions = False
- countries = []
- p = xml.parsers.expat.ParserCreate()
- def start_element(name, attrs):
- global checkPop, checkName, inRegions
- if name == 'citizen-count':
- checkPop = True
- elif not inRegions and name == 'name':
- checkName = True
- elif name == 'regions':
- inRegions = True
- def end_element(name):
- global inRegions
- if name == 'regions':
- inRegions = False
- def char_data(data):
- global checkPop, checkName, population, countries
- if checkPop:
- population = data
- checkPop = False
- elif checkName:
- countries.append((data, int(population)))
- checkName = False
- p = xml.parsers.expat.ParserCreate()
- p.StartElementHandler = start_element
- p.EndElementHandler = end_element
- p.CharacterDataHandler = char_data
- p.Parse(s)
- m = len(countries)
- for a in range(1, m):
- t = countries[a]
- b = a - 1
- while (b >= 0 and t[1] > countries[b][1]):
- countries[b + 1] = countries[b]
- b -= 1
- countries[b + 1] = t
- for c in countries:
- print c[0] + ': ', c[1]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement