2016-07-05 149 views
-2

我想讀一些文件中的數據,並保存到字典 的文件,我讀看起來像下面蟒蛇使用詞典

file_1.txt 
line 1: Given Problem1, Incorrect Answer, Correct Answer, Rule 
line 2: ___blank___ , Incorrect_Answer 
line 3: ___blank___ , Incorrect_Answer 
line 4: Given Problem2, Incorrect Answer, Correct Answer, Rule 
line 5: ___blank___ , Incorrect_Answer 
line 6: ___blank___ , Incorrect_Answer 

所以有一個問題,正確答案和規則但一些Incorrect_Answer

字典的形式,我想看起來像下面 {問題,Incorrect_Answer [...(陣列也許?),Correct_Answer,規則}

我至今。

for line in open(thatfile.txt,'r').readlines()[2:]: # scan through the file, but skip the first two lines that contain metadata (data headers) 
if line[0].startswith('Question '): 
    continue: 
elif line starts with ' ' #there will be only incorrect_answer in this line 
    input line[1] to Incorrect_Answer array 
else 
    input line[0] to Question 
    input line[1] to Incorrect_Answer array 
    input line[2] to Correct_Answer 
    input line[3] to Rule $ 
+0

你對這段代碼有什麼問題?請儘可能具體。 –

+1

這是*完全*文件的樣子? –

回答

0

你要開始與主輸出結果容器和問題佔位符:

>>> result = {} 
>>> question = None 

然後,你做你的正常處理每一行的同時觀察問題的變化,追加不正確的反應:

>>> for line in txtfile: 
... pieces = line.split(',') 
... if pieces[0] not in (question, ' ___blank___ '): 
...  question = pieces[0] 
...  result[question] = {'Incorrect': [pieces[1]], 
...       'Correct': pieces[2], 
...       'Rule': pieces[3]} 
... elif pieces[0].startswith(' '): 
...  result[question]['Incorrect'].append(pieces[1]) 

它給你:

>>> pprint(result) 
{'Given Problem1': {'Correct': ' Correct Answer', 
        'Incorrect': [' Incorrect Answer', 
            ' Incorrect_Answer', 
            ' Incorrect_Answer'], 
        'Rule': ' Rule'}, 
'Given Problem2': {'Correct': ' Correct Answer', 
        'Incorrect': [' Incorrect Answer', 
            ' Incorrect_Answer', 
            ' Incorrect_Answer'], 
        'Rule': ' Rule'}} 
+0

什麼不在(問題,'空白')是什麼意思? –

+0

'不在(問題中,'___blank___')'是一種確定問題何時改變的方法。由於您沒有空白行的問題,因此您將當前問題(或空條目)與上次遇到的問題進行比較。只有當你看到一個你從未見過的新問題時(或第一個問題,當佔位符是'None'時)纔會觸發條件。 –

+0

感謝您的快速回復! 我得到了這個概念,現在知道爲什麼沒有(問題),但仍然有點'混淆___blank___'部分。那麼它是否會檢查棋子[0]是否具有___空白?如果是這樣,它會跳過這個條件嗎? –

0

我會假設 - 正如你在上面寫的那樣 - 問題和答案用逗號分開。

alldata = [] 
# scan through the file, but skip the first two lines 
# that contain metadata (data headers) 
for line in open(thatfile.txt,'r').readlines()[2:]: 
    lineparts = line.split(',') # or how else can one separate the parts? 
    if len(lineparts) == 4: 
     thisset = {'Question': lineparts[0], 
        'Incorrect': [lineparts[1]], 
        'Correct': lineparts[2], 
        'Rule': lineparts[3]} 
     alldata.append(thisset) 
    # if there are not 4 parts and the data is perfect, this is an incorrect answer 
    else: 
     thisset["Incorrect"].append(lineparts[1]) 

print("Incorrect answers to first question:") 
print(alldata[0]["Incorrect"]) 

所以最後,你會得到一個字典'alldata'的列表。每個字典對應一個問題。不正確的答案本身又是一個字符串列表。