-
-
Save stuartathompson/2415494 to your computer and use it in GitHub Desktop.
Revisions
-
stuartathompson revised this gist
Apr 18, 2012 . 1 changed file with 19 additions and 5 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,21 @@ #!/usr/bin/env python import csv import json r = csv.reader(open("data.csv")) headers = r.next() countries = headers[1:] def saveData(output): filename = "ranked" print "Writing JSON output to %s.json" % filename json_file = open('%s.json' % filename, 'w') json_file.write(json.dumps(output)) json_file.close() output = [] with open("ranked.csv","w") as f: w = csv.writer(f) w.writerow(['Area','#1','#2','#3','#4','#5']) @@ -27,15 +40,16 @@ if once == 0: adjust = adjust + 1 once = 1 out_row[rowSelect] = "[" + out_row[rowSelect].replace('[', '').replace(']','') + "," + country + "]" else: adjust = 0 once = 0 out_row.append(country) else: out_row.append(country) if(len(out_row) == 6): break output.append(out_row) w.writerow(out_row) f.close() saveData(output) -
stuartathompson revised this gist
Apr 18, 2012 . 1 changed file with 28 additions and 6 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -9,11 +9,33 @@ for row in r: area = row[0] values = map(int,row[1:]) vc = zip(values,countries) vc.sort() vc.reverse() out_row = [area] vcCount = -1 rowSelect = -1 adjust = 0 once = 0 for value, country in vc: vcCount = vcCount + 1 rowSelect = rowSelect + 1 - adjust data = [] if value != 0: if vcCount-1 != -1: if value == vc[vcCount-1][0]: if once == 0: adjust = adjust + 1 once = 1 out_row[rowSelect] = "[" + out_row[rowSelect].replace('[', '').replace(']','') + ', "' + country + '"]' else: adjust = 0 once = 0 out_row.append('"' + country + '"') else: out_row.append('"' + country + '"') if(len(out_row) == 6): break w.writerow(out_row) f.close() -
JoeGermuska created this gist
Apr 18, 2012 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ #!/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()