python3将文本文件读入单独的词典

时间:2017-04-13 13:29:23

标签: python-3.x dictionary

我试图找出如何将每个块中的以下示例文本文件读入字典,使用日期作为键,其余作为值,但我无法解决:

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

对不起,我没有任何正常工作的代码

2 个答案:

答案 0 :(得分: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']]}

答案 1 :(得分: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个月了,所以我确定这段代码可以清理一下,但它应该包含解决问题所需的工具。