Skip to content

Instantly share code, notes, and snippets.

@stuartathompson
Forked from JoeGermuska/ranker.py
Created April 18, 2012 18:08
Show Gist options
  • Select an option

  • Save stuartathompson/2415494 to your computer and use it in GitHub Desktop.

Select an option

Save stuartathompson/2415494 to your computer and use it in GitHub Desktop.
Example of how to convert a csv file to a table of ranked headers for each row
#!/usr/bin/env python
import csv
r = csv.reader(open("data.csv"))
headers = r.next()
countries = headers[1:]
with open("ranked.csv","w") as f:
w = csv.writer(f)
w.writerow(['Area','#1','#2','#3','#4','#5'])
for row in r:
area = row[0]
values = map(int,row[1:])
values_and_countries = zip(values,countries)
values_and_countries.sort()
values_and_countries.reverse()
out_row = [area]
for value, country in values_and_countries[:5]: # note this doesn't account for ties
out_row.append(country)
w.writerow(out_row)
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment