python3读取文本文件到单独的字典

问题描述:

我想弄清楚如何读取下面的示例文本文件到字典每块使用一天作为关键和其余作为值,但我不能解决它: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 

我很抱歉,我没有任何工作代码

嗯,这是我很快就扔了一个例子,显然该代码可以改善,它创建与键的字典一天中的每一天,并且每天都有一份计划中的每项任务的清单。

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']]} 

我有一个类似的方法是什么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'。你是从上面复制并粘贴代码还是修改一下?如果我能看到代码,我可以帮助解决问题。 –