好吧,我有一個文本文件,「relationships.txt」具有以下內:文本文檔到字典中的Python
Elizabeth: Peter, Angela, Thomas
Mary: Tom
Angela: Fred, Alison
Alison: Beatrice, Dick, Harry
mother Elizabeth
mother Tom
mother Angela
mother Gurgle
第4行設置爲母親:小孩,兒童,兒童等 底部的4行是應該返回結果的語句。例如:
mother Elizabeth
應該返回:Mother not known
而
mother Tom
應該返回:Mary
我打算創建一個字典,讓這個工作,但我不知道該怎麼辦。幫助表示讚賞。
到目前爲止,我有以下幾點:
test_file = open('relationships.txt', 'w')
test_file.write('''Elizabeth: Peter, Angela, Thomas
Mary: Tom
Angela: Fred, Alison
Alison: Beatrice, Dick, Harry
mother Elizabeth
mother Tom
mother Angela
mother Gurgle
''')
test_file.close()
def create_list():
open_file = open('relationships.txt', 'r')
lines = open_file.readlines()
return(lines)
完全不知道呢?你還沒有嘗試過任何東西? – Blender
這可能是一個良好的開端:http://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files – monkut
人們不會爲你做你的問題。你需要發佈代碼,然後人們會幫助你解決你不明白的問題或者錯誤的做法。 –