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.

Revisions

  1. stuartathompson revised this gist Apr 18, 2012. 1 changed file with 19 additions and 5 deletions.
    24 changes: 19 additions & 5 deletions ranker.py
    Original 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 + '"]'
    out_row[rowSelect] = "[" + out_row[rowSelect].replace('[', '').replace(']','') + "," + country + "]"
    else:
    adjust = 0
    once = 0
    out_row.append('"' + country + '"')
    out_row.append(country)
    else:
    out_row.append('"' + country + '"')
    out_row.append(country)
    if(len(out_row) == 6):
    break
    output.append(out_row)
    w.writerow(out_row)

    f.close()
    f.close()
    saveData(output)
  2. stuartathompson revised this gist Apr 18, 2012. 1 changed file with 28 additions and 6 deletions.
    34 changes: 28 additions & 6 deletions ranker.py
    Original 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:])
    values_and_countries = zip(values,countries)
    values_and_countries.sort()
    values_and_countries.reverse()
    vc = zip(values,countries)
    vc.sort()
    vc.reverse()
    out_row = [area]
    for value, country in values_and_countries[:5]: # note this doesn't account for ties
    out_row.append(country)
    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()

    f.close()
  3. @JoeGermuska JoeGermuska created this gist Apr 18, 2012.
    19 changes: 19 additions & 0 deletions ranker.py
    Original 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()