Skip to content

Instantly share code, notes, and snippets.

@saliksyed
Created June 29, 2017 09:54
Show Gist options
  • Save saliksyed/59e785a919cce97162df1fba45db1179 to your computer and use it in GitHub Desktop.
Save saliksyed/59e785a919cce97162df1fba45db1179 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 29 11:26:56 2017
@author: saliksyed
"""
gdp_data = open("gdp.dat").readlines()
gdp_by_country_code = {}
for line in gdp_data:
data = line.rstrip().split(",")
country = data[0]
country_code = data[1]
gdp_values = []
for value in data[5:]:
# Remove the extra quotes in the data files
without_quotes = value[1:-1]
# We need to do this because there is some missing data which is represented by ""
if without_quotes == "":
final_parsed_value = None
else:
final_parsed_value = float(without_quotes)
gdp_values.append(final_parsed_value)
country_data = {}
country_data["country"] = country
country_data["country_code"] = country_code
country_data["gdp_values"] = gdp_values
gdp_by_country_code[country_code] = country_data
population_data = open("raw_worldbank.txt","r").readlines()
population_by_country_code = {}
for line in population_data:
row = line.split("\t")
country = row[2]
country_code = row[3]
population_data = []
population_data_raw = row[4:-1]
for val in population_data_raw:
if val != '..':
population_data.append(float(val))
else:
population_data.append(None)
country_data = {}
country_data["country"] = country
country_data["country_code"] = country_code
country_data["population_values"] = population_data
population_by_country_code[country_code] = country_data
print population_by_country_code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment