Created
July 29, 2015 15:28
-
-
Save name1984/9552608e8b3630660dfa 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
__author__ = 'name1984' | |
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import MySQLdb | |
import json | |
import csv | |
import sys | |
reload(sys) | |
sys.setdefaultencoding("utf-8") | |
bd = MySQLdb.connect('localhost','edxapp001','password','edxapp') | |
# Preparamos el cursor que nos va a ayudar a realizar las operaciones con la base de datos | |
cursor = bd.cursor() | |
# Ejecutamos un query SQL usando el metodo execute() que nos proporciona el cursor | |
cursor.execute("" | |
" select " | |
" courseware_offlinecomputedgrade.gradeset, auth_userprofile.name, auth_userprofile.cedula " | |
" from " | |
" courseware_offlinecomputedgrade " | |
"inner join " | |
" auth_userprofile " | |
"WHERE " | |
" courseware_offlinecomputedgrade.user_id = auth_userprofile.user_id " | |
" and courseware_offlinecomputedgrade.course_id = 'course-v1:mineduc-forma+LMN101+2015_07'") | |
data = cursor.fetchall() | |
rowarray_list = [] | |
aux = {} | |
for item in data: | |
dic = json.loads(item[0]) | |
aux = {'nombres-apellidos': item[1], 'cedula':item[2]} | |
dic.update(aux) | |
rowarray_list.append(dic) | |
preguntas_list = [] | |
preg_tab_list = {} | |
i = 0 | |
with open('grade_report.csv', 'w') as csvfile: | |
i = 0 | |
for dic in rowarray_list: | |
if dic.has_key('section_breakdown'): | |
print "-----------------------------------" | |
print "Participante : %s Cedula : %s" %(dic['nombres-apellidos'], dic['cedula']) | |
fieldnames = ['nombres apellidos', 'cedula', 'grades', j.values()[0] for j in dic['section_breakdown']] | |
if i == 0: | |
writer = csv.DictWriter(csvfile, fieldnames=fieldnames) | |
writer.writeheader() | |
i = i + 1 | |
for test in dic['section_breakdown']: | |
writer.writerow({'nombres apellidos': dic['nombres-apellidos'], 'cedula': dic['cedula'], i.values() for i in test}) | |
print u"%s"%test['detail'] | |
bd.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment