#单选题格式==转为==职教云慕课导入格式
#导出的题目:有题干、答案、选项三个字段
#题目文本文件格式:题干占一行(正确答案在题干中),单选题选项占一行,例如:
'''
1、世界上第一台电子计算机诞生于( B )年。
A)1939 B)1946 C)1952 D)1958
2、冯·诺依曼研制成功的存储程序计算机名叫(A )。
A)EDVAC B)ENIAC C)EDSAC D)MARK-2
'''
import re
import xlwt
with open("D:/python/kst.txt","r",encoding="utf-8") as f:
content = f.readlines()
#print(type(content))
tigans=[]
items=[]
answers=[]
timu=[]
with open("d:/timu.txt","w",encoding="utf-8") as tm:
for line in content:
line = line.strip('\n')
line="".join(line.split()) #删除字符串内的空格,split()默认时空格分隔
#print(type(line))
#题干提取处理
regex = re.compile('^\d{1,}')
m = re.match(regex,line)
#print(m)
if m is not None:
#提取到的题干 #找出答案
if re.findall("A",line):
answer=line[line.index("A")]
tigan=line.replace('A',' ')
if re.findall("B",line):
answer=line[line.index("B")]
tigan=line.replace('B',' ')
if re.findall("C",line):
answer=line[line.index("C")]
tigan=line.replace('C',' ')
if re.findall("D",line):
answer=line[line.index("D")]
tigan=line.replace('D',' ')
tigans.append(tigan)
answers.append(answer)
#题目选项处理
n = re.match('^A)',line)
if n is not None:
itema=line.replace('A)','A:')
itemb=itema.replace('B)',';B:')
itemc=itemb.replace('C)',';C:')
itemd=itemc.replace('D)',';D:')
print(tigan)
#采用文本文件写入csv 格式
tm.write(tigan+',')
tm.write(answer+',')
tm.write(itemd+'\n')
items.append(itemd)
#用函数把整理好的文件写入.xls文件,以便上传
def data_write(file_path, tg,daan,select):
f = xlwt.Workbook()
sheet1 = f.add_sheet(u'sheet1',cell_overwrite_ok=False) #创建sheet
#将数据写入第 i 行,第 j 列
i = 0
for index in range(len(daan)):
sheet1.write(i,0,tg[index])
sheet1.write(i,1,daan[index])
sheet1.write(i,2,select[index])
i = i + 1
f.save(file_path) #保存文件
data_write("d:/tigan.xls",tigans,answers,items)