Login

Retrieve a list of countries from GeoNames

Author:
simon
Posted:
September 11, 2008
Language:
Python
Version:
1.0
Score:
4 (after 4 ratings)

GeoNames provides a useful data file with information about every country in the world (DjangoPeople uses this). Here's an importer that grabs the file from the web and turns it in to a list of dictionaries.

 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
45
46
47
"""
Retrieve a list of information about countries, pulled from GeoNames.

Example entry:

 {u'Area(in sq km)': u'33843',
  u'Capital': u'Chi\u015fin\u0103u',
  u'Continent': u'EU',
  u'Country': u'Moldova',
  u'CurrencyCode': u'MDL',
  u'CurrencyName': u'Leu',
  u'EquivalentFipsCode': u'',
  u'ISO': u'MD',
  u'ISO-Numeric': u'498',
  u'ISO3': u'MDA',
  u'Languages': u'mo,ro,ru,gag,tr',
  u'Phone': u'373',
  u'Population': u'4324000',
  u'Postal Code Format': u'MD-####',
  u'Postal Code Regex': u'^(?:MD)*(\\d{4})$',
  u'fips': u'MD',
  u'geonameid': u'617790',
  u'neighbours': u'RO,UA',
  u'tld': u'.md'}
"""

import urllib, codecs

COUNTRY_INFO_URL = "http://download.geonames.org/export/dump/countryInfo.txt"

def get_geonames_country_data():
    "Returns a list of dictionaries, each representing a country"
    udata = urllib.urlopen(COUNTRY_INFO_URL).read().decode('utf8')
    # Strip the BOM
    if udata[0] == codecs.BOM_UTF8.decode('utf8'):
        udata = udata[1:]
    # Ignore blank lines
    lines = [l for l in udata.split('\n') if l]
    # Find the line with the headers (starts #ISO)
    header_line = [l for l in lines if l.startswith('#ISO')][0]
    headers = header_line[1:].split('\t')
    # Now get all the countries
    country_lines = [l for l in lines if not l.startswith('#')]
    countries = []
    for line in country_lines:
        countries.append(dict(zip(headers, line.split('\t'))))
    return countries

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months ago
  5. Help text hyperlinks by sa2812 11 months, 3 weeks ago

Comments

Please login first before commenting.