2017-04-13 74 views
0

我想弄清楚如何讀取下面的示例文本文件到字典每塊使用一天作爲關鍵和其餘作爲值,但我不能解決它:python3讀取文本文件到單獨的字典

Friday 
10:00 - 10:30 Debrief 
10:30 - 13:00 Track running 
13:00 - 14:00 Lunch 
14:00 - 18:00 Track running 
18:00   End 

Saturday 
10:00 - 10:30 Debrief 
10:30 - 13:00 Track running 
13:00 - 14:00 Lunch 
14:00 - 18:00 Track running 
18:00   End 

Sunday 
10:00 - 10:30 Debrief 
10:30 - 13:00 Track running 
13:00 - 14:00 Lunch 
14:00 - 18:00 Track running 
18:00   End 

這應該產生3個字典星期五,星期六和星期日,每個塊的其餘部分應該是鍵的值。

這就是我已經開始做的,但我堅持如何使用文本文件中的文本作爲字典鍵加上這看起來很麻煩,有沒有一種簡單的方法來做到這一點,或者我在反正右行?:

def schedule(): 
    flag = False 
    with open("example.txt", 'r') as f: 
     for line in f: 
      if len(line.strip()) == 0: 
       flag = True 
      elif flag: 
       # somehow use the day of the week to 
       # become the dictionary key 
       flag = False 
      else: 
       # somehow use the rest of the block of text as the value 

我很抱歉,我沒有任何工作代碼

回答

0

嗯,這是我很快就扔了一個例子,顯然該代碼可以改善,它創建與鍵的字典一天中的每一天,並且每天都有一份計劃中的每項任務的清單。

def schedule(): 
flag = False 
schedule = dict() 
days_of_week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", 
       "Saturday", "Sunday"] 

with open("example.txt", 'r') as f: 
    for line in f: 
     line_items = line.split() 
     if line_items and line_items[0] in days_of_week: 
      schedule[line_items[0]] = list() 
      current_day = line_items[0] 
     else: 
      if line_items and current_day: 
       schedule[current_day].append(line_items) 

return schedule 

這將導致:

{'Sunday': [['10:00', '-', '10:30', 'Debrief'], ['10:30', '-', '13:00', 'Track', 'running'], ['13:00', '-', '14:00', 'Lunch'], ['14:00', '-', '18:00', 'Track', 'running'], ['18:00', 'End']], 'Friday': [['10:00', '-', '10:30', 'Debrief'], ['10:30', '-', '13:00', 'Track', 'running'], ['13:00', '-', '14:00', 'Lunch'], ['14:00', '-', '18:00', 'Track', 'running'], ['18:00', 'End']], 'Saturday': [['10:00', '-', '10:30', 'Debrief'], ['10:30', '-', '13:00', 'Track', 'running'], ['13:00', '-', '14:00', 'Lunch'], ['14:00', '-', '18:00', 'Track', 'running'], ['18:00', 'End']]} 
0

我有一個類似的方法是什麼Ilhicas在使用包含一週的天詞典方面做了。我更進了一步,能夠爲每個字典條目的「值」提供所需信息的字符串。

with open('example.txt', 'r') as f: 
    dictDays = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'] 
    lines = f.read().splitlines() #read lines but removes '\n' 
    schDict = {} 
    for text in lines: 
     if text in dictDays: 
      schDict.update({text: ''}) 
      del lines[lines.index(text)] 
    connect = [''] 
    count = 0 
    for text in lines: 
     if text != '': 
      connect[count] += (text + '\n') 
     if text == '': 
      connect.append('') 
      count += 1 
    for txtNum in range(len(connect)): 
     dayKey = list(schDict.keys())[txtNum] 
     schDict.update({dayKey:connect[txtNum]}) 

print(schDict) 

我也是新來的Python。我一直在使用它大約2個月,所以我確信這塊代碼可以清理一下,但它應該包含解決問題所需的工具。

+0

我不能得到這個工作不幸,如果給列表超出範圍 – iFunction

+0

嗯?這太有趣了。我只是將上面的代碼複製並粘貼到Atom中的一個新的.py文件中,確保open函數指向正確的文件位置並運行它。這是我得到的輸出: '{'Friday':'10:00 - 10:30彙報\ n10:30 - 13:00賽道跑步\ n13:00 - 14:00午餐\ n14:00 - 18:00賽道跑步\ n18:00結束\ n','週六':'10:00 - 10:30結束報道\ n10:30 - 13:00賽道跑步\ n13:00 - 14:00午餐\ n14:00-18: 00 Track running \ n18:00' 由於字符限制,我縮短了輸出,但您明白了。 –

+0

當您試圖通過空列表運行for循環時,會出現'list out of range'。你是從上面複製並粘貼代碼還是修改一下?如果我能看到代碼,我可以幫助解決問題。 –