import csv
import sys

from collections import defaultdict

# These modules can be found under reporting scripts. You will have to add them
# in the same directory as this script
from base_edx import EdXConnection
from generate_csv_report import CSV

db_name = sys.argv[1]
answer_distribution = sys.argv[2]
connection = EdXConnection(db_name, 'tracking' )
collection = connection.get_access_to_collection()

with open(answer_distribution) as f:
    csv_f = csv.reader(f)
    headers = csv_f.next()
    rows = [row for row in csv_f]

answer_keys = dict()

for row in rows:
    problem_id = row[1]
    value = row[4]
    answer_value = row[5]
    if value:
        value = value.replace('[', '').replace(']', '').split('|')
        answer_value = answer_value.replace('[', '').replace(']', '').split('|')
        dict_value_answer = dict(zip(value, answer_value))
        try:
            answer_keys[problem_id].update(dict_value_answer)
        except:
            answer_keys[problem_id] = dict_value_answer
         
student_answers_db = collection['tracking'].find({'event_type': 'problem_check', 'event_source' : 'server'})
for doc in student_answers_db:
    try:
        print doc['event']['state']['student_answers']
    except:
        print "Key Error!!!"

@@ -19,44 +19,19 @@
 sys%0A
-%0Afrom collections import defaultdict
+import json
 %0A%0A# 
@@ -981,18 +981,8 @@
 wer%0A
-         %0A
 stud
@@ -1086,16 +1086,32 @@
 rver'%7D)%0A
+output = dict()%0A
 for doc 
@@ -1154,92 +1154,1819 @@
     
-print doc%5B'event'%5D%5B'state'%5D%5B'student_answers'%5D%0A    except:%0A        print %22Key Error!!!%22
+username = doc%5B%22username%22%5D%0A        problem_id = doc%5B'event'%5D%5B'problem_id'%5D%0A        student_answers =  doc%5B'event'%5D%5B'answers'%5D%0A        student_attempts =  doc%5B'event'%5D%5B'attempts'%5D%0A        student_answers_values = dict()%0A        for key, values in student_answers.iteritems():%0A            if isinstance(values, list):%0A                if %22choice_%22 in %22,%22.join(values):%0A                    mapped_values = answer_keys%5Bkey%5D.values()%0A                    student_answers_values%5Bkey%5D = mapped_values%0A                else:%0A                    student_answers_values%5Bkey%5D = values %0A            else:%0A                if %22choice_%22 in values:%0A                    mapped_values = answer_keys%5Bkey%5D.values()%0A                    student_answers_values%5Bkey%5D = mapped_values%0A                else:%0A                    student_answers_values%5Bkey%5D = values%0A        if username not in output:%0A            output%5Busername%5D = %7B'problem_id' : problem_id, 'student_answers' :  student_answers_values, 'attempts' : student_attempts%7D%0A        else: %0A            if student_attempts %3E output%5Busername%5D%5B'attempts'%5D:%0A                output%5Busername%5D%5B'problem_id'%5D = problem_id%0A                output%5Busername%5D%5B'student_answers'%5D = student_answers_values%0A                output%5Busername%5D%5B'attempts'%5D = student_attempts%0A    except:%0A        print %22Key Error!!!%22, key, values, problem_id%0A%0Aresult = %5B%5D%0Afor key, values in output.iteritems():%0A    username = key%0A    problem_id = values%5B'problem_id'%5D%0A    for question, answer in values%5B'student_answers'%5D.iteritems():%0A        result.append(%5Busername, problem_id, question, answer%5D)%0A        %0Aoutput = CSV(result, %5B'Username','Problem ID', 'Student Answers' %5D, output_file=db_name + '_student_answers.csv')%0Aoutput.generate_csv()%0A%0A#with open(db_name + '_student_answers', 'w') as f:%0A#    json.dump(output, f)
 %0A

Update script to save answers to a csv reports