Created
January 13, 2016 14:57
-
-
Save glfharris/1a1c471043f62813b802 to your computer and use it in GitHub Desktop.
This file contains 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
import csv | |
myfile = "Path/To/WDI_Data.csv" | |
data = {} | |
t = list(range(1960, 2016)) | |
years = [] | |
for item in t: | |
years += [str(item)] | |
temp = [] | |
with open(myfile) as f: | |
myreader = csv.DictReader(f) | |
for row in myreader: | |
temp += [row['Country Name']] | |
countries = list(set(temp)) | |
for country in countries: | |
data[country] = {} | |
for year in years: | |
data[country][year] = {} | |
print("Build Indices") | |
print("Parsing File") | |
with open(myfile) as f: | |
myreader = csv.DictReader(f) | |
for row in myreader: | |
for year in years: | |
data[row['Country Name']][year][row['Indicator Name']] = row[year] | |
fieldnames = list(data['United Kingdom']['1990'].keys()) + ['Country Name', 'Year'] | |
print("outputing file") | |
with open('output.csv', 'w') as output: | |
writer = csv.DictWriter(output, fieldnames=fieldnames) | |
writer.writeheader() | |
for k, v in data.items(): | |
for x, y in v.items(): | |
y['Country Name'] = k | |
y['Year'] = x | |
writer.writerow(y) | |
print("Finished") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment