Python工具/软件包,从文本文件中的问答选项中生成测验



我有一个文本文件,该文件在以下模式中有30个多项选择问题

  1. 问题一个问题吗?

    a。选项1

    b。选项2

    c。选项3

    d。选项4

等于30

选项数量是可变的;至少有两个和最多六个选项。

我想在诸如HTML/PHP测验之类的接口中练习这些问题,该界面使我可以选择选项并终于显示结果。

我尝试在python中读取文件,然后尝试将问题和答案存储在单独的列表中,但它不起作用。以下是我的代码:

#to prevent IndexError 
question = ['','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','']
answers = ['','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','']
qOrA = "q"
mcq_file = "mcqs.txt"
mcq = open(mcq_file, "r")
data_list = mcq.readlines()
for i in range(len(data_list)):
    element = list(data_list[i])
    if element[0] == "A" and element[1] == ".":
        qOrA = "a"
    if qOrA == "q":
        question[i] = question[i]+ " " + data_list[i]
    elif qOrA == "a":
        answers[i] = answers[i]+ " " + data_list[i]

mcq.readlines()输出到问题编号。3在下面给出注意:实际上有多个线路断路,因此文件结构不正确。

['n', '1.n', 'n', ' n', 'n', 'Which computer component contains all the n', 'n', 'circuitry necessary for all components or n', 'n', 'devices to communicate with each other?n', 'n', ' n', 'n', ' n', 'n', ' n', 'n', ' n', 'n', ' n', 'n', 'A. Motherboardn', 'n', ' n', 'n', ' n', 'n', 'B. Hard Driven', 'n', ' n', 'n', ' n', 'n', 'C. Expansion Busn', 'n', ' n', 'n', ' n', 'n', 'D. Adapter Cardn', 'n', ' n', 'n', ' n', 'n', 'n', 'n', 'n', '2. n', 'n', 'Which case type is typically n', 'n', 'used for servers?n', 'n', ' n', 'n', ' n', 'n', ' n', 'n', ' n', 'n', ' n', 'n', 'A.n', 'n', ' n', 'n', ' n', 'n', 'Mini Towern', 'n', ' n', 'n', ' n', 'n', 'B.n', 'n', ' n', 'n', ' n', 'n', 'Mid Towern', 'n', ' n', 'n', ' n', 'n', 'C.n', 'n', ' n', 'n', ' n', 'n', 'Full Towern', 'n', ' n', 'n', ' n', 'n', 'D.n', 'n', ' n', 'n', ' n', 'n', 'desktopn', 'n', ' n', 'n', ' n', 'n', ' n', 'n', ' n', 'n', ' n', 'n', ' n', 'n', 'n', 'n', 'n', '3.n', 'n', ' n', 'n', 'What is the most reliable way for users to buy the n', 'n', 'correct RAM to upgrade a computer?n', 'n', ' n', 'n', ' n', 'n', ' n', 'n', ' n', 'n', 'A.n', 'n', ' n', 'n', ' n', 'n', 'Buy RAM that is the same color as the memory sockets n', 'n', 'on the motherboard.n', 'n', ' n', 'n', ' n', 'n', 'B.n', 'n', ' n', 'n', ' n', 'n', 'Ensure that the RAM chip is the same size as the ROM chip.n', 'n', ' n', 'n', ' n', 'n', 'C.n', 'n', ' n', 'n', ' n', 'n', 'Ensure that the RAM is n', 'n', 'compatiblen', 'n', ' n', 'n', 'with the peripherals n', 'n', 'installed on the motherboard.n', 'n', ' n', 'n', ' n', 'n', 'D.n', 'n', ' n', 'n', ' n', 'n', 'Check the motherboard manual or manufacturer’s website.n', 'n', ' n', 'n', ' n', 'n', ' n', 'n', ' n', 'n', 'n', 'n', 'n']

您可以尝试:

question =[]
mcq = open(mcq_file, "r")
data_list = mcq.readlines()  
for data in data_list:
    data = data.strip() #removes white space
    first_part = data.split(".")[0] 
    if first_part.isnumeric():
        # This is question and you can append this in question list
        question.append(data)

希望这可以做您想要的。有一些小错误,因为MCQS.TXT中的格式是不可预测的。例如,在问题5中,答案出现了。当我们选择容纳问题26的较低案例的答案选择时,我们将一个邪恶交换为另一个邪恶,这就是为什么问题3答案选择看起来很奇怪("网站。"one_answers"主板"。以" e。"one_answers" d结尾"。")。同样,问题25是编写2 n 5.这就是为什么偶尔在输出中串联的单独单词的原因。我很想知道原始文档的格式以及为什么它如此混乱?这些错别字还是您从PDF粘贴或其他?

#Get text into workable format
txt=open(r"mcqs.txt","r").readlines()
txt=[line.replace("n"," ") for line in txt]
txt=[line for line in txt if len(line)>0]
txt=[line.encode('ascii','ignore').decode("utf-8") for line in txt]
txt=[line.strip() for line in txt if line!=" " and line!=""]
txt1="".join(txt)     
#Initialize Separator lists
full_test,q_list,let_list=dict(),[str(i)+"." for i in range(1,31)],["A","B","C","D","E","F"]
def segment(txt1,list_of_separators):
    #Returns list of tuples
    #Tuples define start and end index of separator
    i,j,ints,ends=0,0,[],[]
    while j<len(list_of_separators):
        sep=list_of_separators[j]
        if sep in txt1[i:i+len(sep)+1] or sep.lower() in txt1[i:i+len(sep)+1]:
            index=i+len(sep)
            if txt1[i+len(sep)]==".": index=index+1
            ints.append(index)
            ends.append(len(sep))
            j=j+1
        if i==len(txt1):
            break
        i=i+1
    ints=ints+[len(txt1)+ends[-1]]
    tups = [(ints[k],ints[k+1]-ends[k]) for k in range(len(ints)-1)]
    return tups
#Segment based on question number
tups=segment(txt1,q_list)
#Get blocks of text (includes question and answer choices)
blocks,n=[txt1[tup[0]:tup[1]].strip() for tup in tups],1
for block in blocks:
    #Segment based on answer choice
    tups=segment(block,[str(i)+"." for i in let_list])
    tups=[(0,tups[0][0]-2)]+tups
    choices=[block[tup[0]:tup[1]].strip() for tup in tups]
    #Initialize dictionary
    full_test[n]={"Question":choices[0]}
    m=0
    for choice in choices[1:]:
        full_test[n].update({let_list[m]+".":choice})
        m=m+1
    n=n+1
#Prompt user for answer as if actually test
for question in full_test.keys():
    print(str(question)+"."+full_test[question]["Question"]+"n")
    ind=0
    for choice in full_test[question].items():
        if ind==0:
            ind=ind+1
            continue
        else:
            print(choice[0]+" "+choice[1])
    answer=input("nAnswer:")
    full_test[question].update({"Answer":answer})

如果我不为挑战感到满意,我会以格式修复小小的矛盾性,并且可能会提出一些简单的事情。但是,这是什么乐趣...

最新更新