简体   繁体   English

Python-从txt文件创建字典

[英]Python - Create a dict from a txt file

We are given a file (.txt) in the form: 我们将获得以下形式的文件(.txt):

Johnson, Joana
Volleyball Club
Erickson, John
Mcdonald, Joe

Smith, Johnny
Debate Club
Chess Club
McIlroy, Molly
Dino, Dennis

Jackson, Jamie
Gibson, Ginny
Fried, John

I have to write a function which calls this file and returns a dictionary in the form: {'first person's name' : [list of friends within each stanza] so this should be returned: 我必须编写一个函数来调用该文件,并以以下形式返回字典:{'first person's name':[每个节中的朋友列表],因此应返回:

{'Johnson, Joana': ['Erickson, John', 'Mcdonald, Joe'], 'Smith, Johnny': ['McIlroy, Molly', 'Dino, Dennis'], 'Jackson, Jamie': ['Gibson, Ginny', 'Fried, John']} {''约翰逊,乔安娜':['埃里克森,约翰','麦当劳,乔'],'史密斯,约翰尼':['麦克罗伊,莫莉','迪诺,丹尼斯'],'杰克逊,杰米':['吉布森,Ginny','Fried,John']}

I wrote a function below but it only processes the first stanza of the file instead of all of it, so it returns: 我在下面编写了一个函数,但是它仅处理文件的第一个节而不是全部,因此它返回:

{'Johnson, Joana': ['Erickson, John', 'Mcdonald, Joe']} {'约翰逊,乔安娜':['埃里克森,约翰,'麦当劳,乔']}

I am only a beginner in python so if someone can help me without complicating it I would really appreciate it, I cannot seem to process the whole file 我只是python的初学者,所以如果有人能在不复杂的情况下为我提供帮助,我将非常感谢,我似乎无法处理整个文件

def name_to_friends(file):

    '''(file open for reading) -> dict of {str: list of str}
    '''

    for line in file:
        dic = {}
        lst = []
        for line in file:
            if line == '\n':
                dic.update({lst[0]:lst[1:]})
                break
            else:
                name = line.strip()        
                if ',' in line:
                    lst.append(line)
    return dic

You're almost there; 你快到了; remove the break ; 删除break ; and clear the list object each time you add another name and friends to your dictionary: 并在每次向词典中添加其他名称和朋友时清除列表对象:

def name_to_friends(file):
    for line in file:
        dic = {}
        lst = []
        for line in file:
            if line == '\n':
                dic.update({lst[0]:lst[1:]})
                lst = []
            else:
                name = line.strip()        
                if ',' in line:
                    lst.append(line)

        if lst:
            dic.update({lst[0]:lst[1:]})

    return dic

The last if lst is needed for when there is no empty line at the end of the file. if lst文件末尾没有空行,则需要最后一个if lst

Your break statement would stop reading the file altogether the first time you encountered an empty line; 第一次遇到空行时, break语句将完全停止读取文件; by removing it you get to continue to the next block. 通过删除它,您可以继续进行下一个步骤。

A more idiomatic method would be: 更加惯用的方法是:

def name_to_friends(file):
    dic = {}

    for line in file:
        line = line.strip()
        # skip empty lines until we find the start of a block
        if not line:
            continue

        friends = dic[line] = []
        for line in file:
            line = line.strip()
            if not line:
                break  # end of block, continue to the next list of friends
            if ',' in line:
                friends.append(line)

    return dic

This nests a second loop over the file lines inside the first; 这将在第一个文件行中嵌套第二个循环; this advances the file line reading position too, so when the inner loop stops (because the file is done or we just read an empty line), the outer loop will continue reading where we left off. 这也提高了文件行的读取位置,因此当内部循环停止时(因为文件已完成或我们只读取了一个空行),外部循环将继续从中断处继续读取。

if the file is not too large you could simply do something like 如果文件不是太大,您可以执行以下操作

{k[0]: k[1:] for k in [l.split('\n') for l in file.read().split('\n\n')]}

Edit: to remove clubs (no comma) you could 编辑:删除俱乐部(无逗号),您可以

{k[0]: [fr for fr in k[1:] if ',' in fr] for k in [ln.split('\n') for ln in file.read().split('\n\n')]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM