(c) Karen Belita
Team Neighborhood Change
Last Updated 9/7/2016

NOTEBOOK: INGESTION for CBSA-CITY-STATE

To create a table that has the CBSA code for City and State abbreviation that belongs to that MSA that. MSA = Metropolitan Statistical Area

OUTPUT

MSA_principal.xls - contains cbsa code and corresponding msa, and the cities that belong to it (fips state code only)
MSA_STATE.xls - contains state abbreviation and its fips code

DEPENDENCIES


In [6]:
import os
import requests
import pandas as pd
import csv
import urllib2
import openpyxl
import csv

INGESTION

DEFINE FUNCTIONS

Define function that downloads census xls file that contains state abbreviation and state fips code


In [3]:
def xls_state():

    path_year = os.path.join(os.getcwd())
    file_name = path_year + "/" + "MSA_STATE"+ ".xls" 
    url= "https://www.census.gov/2010census/xls/fips_codes_website.xls"

    f = urllib2.urlopen(url)
    data = f.read()
    with open(file_name, "wb") as code:
        code.write(data)

Define function taht downloads census xls file that contains cbsa and the corresponding msa name and principal cities that belong to that msa


In [4]:
def xls_principal():

    path_year = os.path.join(os.getcwd())
    file_name = path_year + "/" + "MSA_principal"+ ".xls" 
    url= "http://www.census.gov/population/metro/files/lists/2015/List2.xls"

    f = urllib2.urlopen(url)
    data = f.read()
    with open(file_name, "wb") as code:
        code.write(data)

MAIN EXECUTION


In [5]:
def main():
    """
    Main execution
    """
    xls_state()       
    xls_principal() 


#######################
### Execution ########
#######################

if __name__ == '__main__':
    main()

In [ ]: