Guest User

get cities

a guest
Aug 2nd, 2011
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  1. import urllib2
  2. import re
  3. import os.path
  4.  
  5. def major_centres(url='http://www.census.gov/popest/cities/tables/SUB-EST2009-01.csv'):
  6.     city_map = [" ".join(x.split(",")[1].split(' ')[:-1]) for x in urllib2.urlopen(url)][4:-5]
  7.     return [x.lower() for x in city_map]
  8.  
  9. def all_population_centres_us():
  10.     """download census data file from http://www.census.gov/popest/cities/files/SUB-EST2009-ALL.csv -- 7M, it might take some time
  11.     Returns list of ('city', 'state') tuples
  12.     """
  13.     centre_data = open('SUB-EST2009-ALL.csv', 'r')
  14.     centre_data.readline()
  15.     centres = []
  16.     for k in (tuple(k.split(",")[5:7]) for k in centre_data.readlines()):
  17.         for term in ('town', 'city'):
  18.             if term in k[0]:
  19.                 k = (k[0].split(term)[0].strip(), k[1])
  20.         centres.append(k)
  21.     centre_data.close()
  22.     return centres
Advertisement
Add Comment
Please, Sign In to add comment