实战:基于xlrd模块实现考试系统管理
import xlrd
data= xlrd. open_workbook( "data2.xlsx" )
sheet= data. sheet_by_index( 0 )
questionList= [ ]
class Question :
pass
for i in range ( sheet. nrows) :
if i> 1 :
obj= Question( )
obj. subject= sheet. cell( i, 1 ) . value
obj. questionType = sheet. cell( i, 2 ) . value
obj. optionA = sheet. cell( i, 3 ) . value
obj. optionB = sheet. cell( i, 4 ) . value
obj. optionC = sheet. cell( i, 5 ) . value
obj. optionD = sheet. cell( i, 6 ) . value
obj. score = sheet. cell( i, 7 ) . value
obj. answer = sheet. cell( i, 8 ) . value
questionList. append( obj)
print ( questionList)
from mysqlhelper import *
db= dbhelper( '127.0.0.1' , 3306 , "root" , "123456" , "test" )
sql= "insert into question(subject,questionType,optionA,optionB,optionC,optionD,score,answer) VALUES (%s,%s,%s,%s,%s,%s,%s,%s)"
val= [ ]
for item in questionList:
val. append( ( item. subject, item. questionType, item. optionA, item. optionB, item. optionC, item. optionD, item. score, item. answer) )
db. executemanydata( sql, val)