Created
November 17, 2022 12:56
-
-
Save rruntsch/049de59aeace0f7143fee875272da9ea to your computer and use it in GitHub Desktop.
Python Program to Retrieve International Database Population Estimates from the Census Data API International Database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Name: c_country_pop.py | |
# Date: November 15, 2022 | |
# Author: Randy Runtsch | |
# | |
# Description: | |
# | |
# Get the mid-year population estimates for a country for the specified year. | |
# Write the data to a CSV file. | |
import requests | |
class c_country_pop: | |
def __init__(self, out_file_nm, country_code, year, sex_code, write_type, api_key): | |
# Set the base URL. | |
self.base_url = 'https://api.census.gov/data/timeseries/idb/1year?get=NAME,AGE,POP' | |
# Set the parameter variables for the API request. | |
self.out_file_nm = out_file_nm | |
self.country_code = country_code | |
self.year = year | |
self.sex_code = sex_code | |
self.write_type = write_type | |
self.api_key = api_key | |
data_in = self.get_data() | |
self.write_data_to_csv(data_in) | |
def get_data(self): | |
# Build the full URL. Then, request the data through the Census Data API. | |
# Create full URL and retrieve data structure in JSON format by calling API with get() function. | |
full_url = self.base_url + '&GENC=' + self.country_code + '&YR=' + str(self.year) + '&SEX=' + str(self.sex_code) + '&key=' + self.api_key | |
data_in_structure = requests.get(full_url) | |
# Convert data content from binary to string. | |
data_in = data_in_structure.content.decode('utf-8') | |
return data_in | |
def write_data_to_csv(self, data_in): | |
# Convert the data from the nonstandard JSON format to CSV records. Write | |
# each record to the specified output file. | |
with open(self.out_file_nm, mode = self.write_type, newline = '') as data_file: | |
rec_no = 0 | |
for record_in in data_in.splitlines(): | |
rec_no = rec_no + 1 | |
# Pass from the loop for one iteration if this is the header (first) record | |
# and the write type is append ('a'). | |
if rec_no == 1 and self.write_type == 'a': | |
continue | |
# Strip leading and trailing brackets ([ and ]) and trailing commas from record. | |
record_out = record_in.lstrip('[[') | |
record_out = record_out.lstrip('[') | |
record_out = record_out.rstrip('],') | |
record_out = record_out.rstrip(']]') | |
# Add a new line to the record out | |
record_out = record_out + '\n' | |
#Write the CSV record to the output file. | |
data_file.write(record_out) | |
data_file.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Name: get_population_estimates.py | |
# Date: November 15, 2022 | |
# Author: Randy Runtsch | |
# | |
# Description: | |
# | |
# Use the c_population_estimates class to obtain the population | |
# of countries by year, gender, and age. | |
from c_country_pop import c_country_pop | |
# | |
# Call the c_population_estimates constructor with these parameters: | |
# | |
# - Folder and JSON file name where debt data will be written. | |
# - Two-character country code. | |
# - Year for population estimate. | |
# - "w" to write records to a new file, or "a" to append records to an existing file. | |
# - Census data API key. | |
# | |
c_country_pop('c:/population_data/pop_2022.csv', 'CN', 2022, 1, 'w', 'YOUR_KEY_GOES_HERE') | |
c_country_pop('c:/population_data/pop_2022.csv', 'CN', 2022, 2, 'a', 'YOUR_KEY_GOES_HERE') | |
c_country_pop('c:/population_data/pop_2022.csv', 'JP', 2022, 1, 'a', 'YOUR_KEY_GOES_HERE') | |
c_country_pop('c:/population_data/pop_2022.csv', 'JP', 2022, 2, 'a', 'YOUR_KEY_GOES_HERE') | |
c_country_pop('c:/population_data/pop_2022.csv', 'NO', 2022, 1, 'a', 'YOUR_KEY_GOES_HERE') | |
c_country_pop('c:/population_data/pop_2022.csv', 'NO', 2022, 2, 'a', 'YOUR_KEY_GOES_HERE') | |
c_country_pop('c:/population_data/pop_2022.csv', 'US', 2022, 1, 'a', 'YOUR_KEY_GOES_HERE') | |
c_country_pop('c:/population_data/pop_2022.csv', 'US', 2022, 2, 'a', 'YOUR_KEY_GOES_HERE') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment