Files
jugement_majoritaire/code/convert_nextcloud_poll_csv.py
2025-09-28 09:50:55 +02:00

68 lines
1.8 KiB
Python

import json
import sys
import csv
# using non modified nextcloud standard text poll
mentions_name=['yes','maybe','','no']
if len(sys.argv) > 1:
csv_file=sys.argv[1]
if len(sys.argv) > 2:
mentions_name=[]
for i in range(2,len(sys.argv)):
mentions_name.append(sys.argv[i])
else:
print('missing file argument')
exit(1)
def mention_index(mention):
return mentions_name.index(mention)
verbose=False
mentions=len(mentions_name)
default_mention=mentions-1
warnings=[]
candidates={}
candidates_alias=[]
a_vote={}
votes=[]
with open(csv_file,'r') as csv_fd:
vote_reader = csv.reader(csv_fd, delimiter=',')
for row in vote_reader:
if len(candidates_alias) == 0:
for i in range(2,len(row)):
candidate_alias=row[i]
candidates_alias.append(candidate_alias)
# no other name given
if not candidate_alias in candidates:
candidates[candidate_alias]=candidate_alias
else:
# check if not duplicated
participant=row[0]
if participant in a_vote:
warnings.append("Mutliple vote " + participant + " no record")
continue
a_vote[participant]=row[1]
vote={}
for i in range(2,len(row)):
candidate_index=i-2
candidate_alias=candidates_alias[candidate_index]
mention=row[i]
vote[candidate_alias]=mention_index(mention)
votes.append(vote)
default_vote={}
if verbose:
print(','.join(candidates))
print(','.join(mentions_name))
result = {'candidates':candidates,'voters':{'count':len(a_vote)},'mentions':mentions_name,'votation':{'votes':votes}}
print(json.dumps(result))
sys.exit(0)