【发布时间】:2017-04-13 13:29:23
【问题描述】:
我正在尝试弄清楚如何将以下示例文本文件读取到每个块的字典中,使用日期作为键,其余的作为值,但我无法解决:
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
对不起,我没有任何工作代码
【问题讨论】:
标签: python-3.x dictionary