Skip to content

Instantly share code, notes, and snippets.

@glfharris
Created January 13, 2016 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glfharris/1a1c471043f62813b802 to your computer and use it in GitHub Desktop.
Save glfharris/1a1c471043f62813b802 to your computer and use it in GitHub Desktop.
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