Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You really don't need two scripts for the task if the only difference is whether the ranking is to be done ascending or descending order -- just make it an argument to a function as shown. The<code>StrCount</code> class is so trivial, it's probably wasn't worth the effort (but I've left it in).</p> <pre><code>import csv from itertools import count, groupby import sys _MIN_INT, _MAX_INT = -sys.maxint-1, sys.maxint RANK_DOWN, RANK_UP = False, True # larger numbers to get higher or lower rank class StrCount(count): """ Like itertools.count iterator but supplies string values. """ def next(self): return str(super(StrCount, self).next()) def rerank(filename, direction): with open(filename, 'rb') as inf: reader = csv.reader(inf) subst = _MIN_INT if direction else _MAX_INT # subst value for empty cells for dataset, rows in groupby(reader, key=lambda row: row[:2]): ranking = StrCount(1) prev = last_rank = None for row in sorted(rows, key=lambda row: int(row[2]) if row[2] else subst, reverse=direction): row[4] = (ranking.next() if row[2] or not row[2] and prev != '' else last_rank) print ','.join(row) prev, last_rank = row[2], row[4] if __name__ == '__main__': print 'CSV example_1.csv (ranked down):' rerank('example_1.csv', RANK_DOWN) print '\nCSV example_2.csv (ranked up):' rerank('example_2.csv', RANK_UP) print '\nCSV example_3.csv (ranked up):' rerank('example_3.csv', RANK_UP) </code></pre> <p>The output:</p> <pre class="lang-none prettyprint-override"><code>CSV example_1.csv (ranked down): uniquedata1,uniquecell1,13,data,1,data uniquedata1,uniquecell1,32,data,2,data uniquedata1,uniquecell1,42,data,3,data uniquedata2,uniquecell2,22,data,1,data uniquedata2,uniquecell2,39,data,2,data uniquedata2,uniquecell2,41,data,3,data uniquedata2,uniquecell2,45,data,4,data uniquedata1,uniquecell2,36,data,1,data uniquedata1,uniquecell2,40,data,2,data uniquedata1,uniquecell2,66,data,3,data CSV example_2.csv (ranked up): uniquedata1,uniquecell1,42,data,1,data uniquedata1,uniquecell1,32,data,2,data uniquedata1,uniquecell1,13,data,3,data uniquedata2,uniquecell2,45,data,1,data uniquedata2,uniquecell2,41,data,2,data uniquedata2,uniquecell2,39,data,3,data uniquedata2,uniquecell2,22,data,4,data uniquedata1,uniquecell2,66,data,1,data uniquedata1,uniquecell2,40,data,2,data uniquedata1,uniquecell2,36,data,3,data CSV example_3.csv (ranked up): uniquedata1,uniquecell1,42,data,1,data uniquedata1,uniquecell1,13,data,2,data uniquedata1,uniquecell1,,data,3,data uniquedata2,uniquecell2,41,data,1,data uniquedata2,uniquecell2,22,data,2,data uniquedata2,uniquecell2,,data,3,data uniquedata2,uniquecell2,,data,3,data uniquedata1,uniquecell2,66,data,1,data uniquedata1,uniquecell2,40,data,2,data uniquedata1,uniquecell2,36,data,3,data </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload