从文件创建元组的dict

时间:2016-04-04 23:01:11

标签: python list file dictionary

我有一个这样的文件:

利默里克

8 A

5 B

Quintain(英文)

0 A

0 B

我想把它变成这样的字典:

{' Limerick':([8,5],[' A',' B']),' Quintain(英文)& #39; :( [0,0],[' A',' B'])}

到目前为止,我已经得到了:

{' Limerick':[],' Rondeau':[],' Haiku':[],' Quintain(英文)&# 39;:[],' Sonnet':[]},

但在那之后我不知道如何追加8 A,5 B等。尝试获取它们开始和停止的位置的索引,但它似乎并不像Python的IO允许的那样。 / p>

假设我试图将8 A,5 B添加到列表中,然后将8,5和A,B添加到两个列表中,然后将它们排序。但这似乎不可能/非常无效。

我的尝试(poem_form是我设法得到的):

def read_poetry_form_descriptions(poetry_forms_file):

    poem_file = open(poetry_forms_file, 'r')
    temp_poem, poem_form = {}, {}

    for line in poem_file:
        temp_poem[line.strip()] = ()
        poem_form.pop('', None)

    poem_file.close()

    for key in temp_poem:
        if key[0:3].isalpha():
            poem_form[key] = []

    print(poem_form)

1 个答案:

答案 0 :(得分:2)

以下是您的问题的可能解决方案。

def read_poetry_from_desc(poetry_forms_file):
    poem_form = {}

    with open(poetry_forms_file, 'r') as f:
        cur_header = None # latest header found in file

        for line in f:
            line = line.strip()

            # Skip empty lines
            if len(line) == 0:
                continue

            if line[0].isalpha():
                # Found new header, add empty entry to dict
                cur_header = line
                poem_form[cur_header] = ([], [])

            else:
                # Found data, record it
                pair = line.split() # split on space

                data = poem_form[cur_header]                
                data[0].append(int(pair[0]))
                data[1].append(pair[1])

    return poem_form

编辑:
这个想法是你知道信息时填充字典。您知道文件的布局是一个标签,后面的数据属于该标签,直到找到另一个标签。

这也可以提高效率,因为数据可以采用的形式非常有限。它是由字母字符或数据组成的标签,以数字开头。因此,我们可以通过查看行是否以字母字符开头来区分这两者。

因为字典poem_form中每个键的值都具有格式([], []),所以当我们在文件中看到新标签时,它会向字典添加空列表。我们还记录了我们目前正在为该特定标签(cur_header)累积。

每当我们看到数据时,它就会被分解并累积在当前标签中(cur_label)。

有关with的说明,请参阅此link。它可以比我更好地解释它。实际上,当你有一些文本块在其使用的开始和结束时完成操作时,使用with。在这里,我将它用于open。通常,您必须打开文件,并在完成后关闭它。在此函数中,退出with范围时,文件将自动关闭。要了解发生这种情况的原因,请参阅链接的文章。

相关问题