Created
August 3, 2009 18:51
-
-
Save mmalone/160756 to your computer and use it in GitHub Desktop.
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 characters
import csv | |
import sys | |
def csv_to_openstv(filename): | |
reader = csv.reader(open(filename)) | |
candidates = reader.next() | |
stv = [] | |
stv.append('%s __seats__' % len(candidates)) | |
for line, row in enumerate(reader): | |
row = [i for i in row if i] | |
if len(row) != len(candidates): | |
print >>sys.stderr, 'WARNING: Incomplete ballot on line %s' % (line + 2) | |
continue | |
if len(set(row)) != len(row): | |
print >>sys.stderr, 'WARNING: Duplicate ranking in ballot on line %s' % (line + 2) | |
continue | |
stv.append('1 %s 0' % ' '.join(row)) | |
stv.append('0') | |
for candidate in candidates: | |
stv.append('"%s"' % candidate) | |
stv.append('__title__') | |
return '\n'.join(stv) | |
if __name__ == '__main__': | |
if len(sys.argv) != 2: | |
print >>sys.stderr, 'usage: csv2openstv.py csv_file_name' | |
sys.exit(1) | |
print csv_to_openstv(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment