Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pnettto/75ccc3323d052d0321588fecf9823ffd to your computer and use it in GitHub Desktop.
Save pnettto/75ccc3323d052d0321588fecf9823ffd to your computer and use it in GitHub Desktop.
"""
This script takes a csv file exported from Google Spreadsheets
after getting answers from a Google Form, and displays the results
neatly in an Markdpown file. It also aggregates the results by one
column (in my case the question "Group name"), which you need to
specify in the group_column variable.
The answers.csv file (from Google Spreasheets) needs to be in the
same directory as this python script on run time.
"""
import csv
questions = []
answers = []
group_column = 2 # Number of CSV column for the group name
# Populate questions and answers
with open('answers.csv', newline='') as csv_file:
reader = csv.reader(csv_file, delimiter=',', quotechar='"')
for i, row in enumerate(reader):
if (i == 0):
questions = row
else:
answers.append(row)
# Create groups structure
groups = {}
for answer in answers:
for index in range(len(questions)):
group_name = answer[group_column]
# Create each group
if group_name not in groups:
# Add pairs to group
pairs = []
for question in questions:
pairs.append({
'question': question,
'answers': []
})
groups[group_name] = pairs
# Add answers to group's pairs
for answer in answers:
group_name = answer[group_column]
for index in range(len(questions)):
groups[group_name][index]['answers'].append(answer[index])
f = open('results.md', 'w')
for group_name, group_pairs in groups.items():
f.write('---------------\n')
f.write(f'# {group_name}\n') # Group title
f.write('---------------\n')
f.write('\n')
for pair in group_pairs:
f.write(f' ## {pair["question"]}') # Question title
f.write('\n')
for answer in pair['answers']:
f.write(f' - {answer}') # Answers
f.write('\n')
f.write('\n')
f.write('---------------\n')
f.write('\n')
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment