2017-12-14 7 views
1

私は1つをここに次のパターンのPythonツール/パッケージ

  1. 質問に30問の選択問題を持っているテキストファイルがありますか?

    A.オプション1

    B.オプション2

    C.オプション3

    D.オプション4

などの30

数に

オプションは可変です。最小2つと最大6つのオプションがあります。

私はオプションを選択して最終的に結果を表示することができる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. Motherboard\n', '\n', ' \n', '\n', ' \n', '\n', 'B. Hard Drive\n', '\n', ' \n', '\n', ' \n', '\n', 'C. Expansion Bus\n', '\n', ' \n', '\n', ' \n', '\n', 'D. Adapter Card\n', '\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 Tower\n', '\n', ' \n', '\n', ' \n', '\n', 'B.\n', '\n', ' \n', '\n', ' \n', '\n', 'Mid Tower\n', '\n', ' \n', '\n', ' \n', '\n', 'C.\n', '\n', ' \n', '\n', ' \n', '\n', 'Full Tower\n', '\n', ' \n', '\n', ' \n', '\n', 'D.\n', '\n', ' \n', '\n', ' \n', '\n', 'desktop\n', '\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', 'compatible\n', '\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'] 
+0

mcq_fileには何が入っていますか? – Harsha

+0

@Harshaそれは '1の形式で30個の選択式質問と回答を含んでいます。質問1はここに行く? 'A.オプション1 'B.オプション2 'C.オプション3 'D.オプション4 '2。質問2はここに行く? 'A.オプション1 'B.オプション2 'C.オプション3 'D.オプション4 など30 –

+0

に、その質問に対する回答が保存されていますか? – Harsha

答えて

0

あなたは試すことができます:

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) 
0

がうまくいけば、これはあなたが欲しいものを行います。 mcqs.txtの書式設定が予期しないほど矛盾しているため、小さなミスがあります。たとえば、質問5では、回答が順不同で表示されます。質問26の小文字の回答の選択肢を取り入れることを選択したときに、私たちは別の悪を交換します。これは質問3の回答の選択肢が奇妙に見える(「ウェブサイト」と「マザーボード」が「e」と「d。 ")。同様に、質問25は2 \ n5と書かれています。これが、別々の単語が時々出力に連結される理由です。私は元の文書の形式が何で、なぜそれほど混乱しているのか不思議です。これらのタイプミスはありますか、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}) 

私は挑戦のためにアップ感じていなかった場合、私はちょうど形式でマイナーな矛盾を固定しているだろうし、おそらく単純なものが出ているでしょう。しかし、その中で楽しいことは何ですか...

+0

実際にファイルが適切に注文されていないため、 "?"行の最後にサインインしてください。この実装はうまくいきません。 –

+0

Ok ...「if」を置き換えることはできますか?答えがタブで表示されているかどうかによって、txt [i] .startswith( "")== Falseの場合、txt [i] 'で' if txt [i] .split( "。")[0] .isnumeric() –

+0

正しく注文されていないということはどういう意味ですか? –