简体   繁体   English

从文本文件创建字典时出错

[英]Error when creating dictionaries from text files

I've been working on a function which will update two dictionaries (similar authors, and awards they've won) from an open text file. 我一直在研究一个功能,该功能将从一个打开的文本文件中更新两个词典(相似的作者,以及他们所获得的奖项)。 The text file looks something like this: 文本文件如下所示:

Brabudy, Ray  
Hugo Award  
Nebula Award  
Saturn Award  
Ellison, Harlan  
Heinlein, Robert  
Asimov, Isaac  
Clarke, Arthur    

Ellison, Harlan  
Nebula Award  
Hugo Award  
Locus Award  
Stephenson, Neil  
Vonnegut, Kurt  
Morgan, Richard  
Adams, Douglas

And so on. 等等。 The first name is an authors name (last name first, first name last), followed by awards they may have won, and then authors who are similar to them. 名字是作者姓名(姓氏名,姓氏名),其次是他们可能获得的奖项,然后是与他们相似的作者。 This is what I've got so far: 到目前为止,这是我得到的:

def load_author_dicts(text_file, similar_authors, awards_authors):
    name_of_author = True
    awards = False
    similar = False
    for line in text_file:
        if name_of_author:
            author = line.split(', ')
            nameA = author[1].strip() + ' ' + author[0].strip()
            name_of_author = False
            awards = True
            continue
        if awards:
            if ',' in line:
                awards = False
                similar = True
            else:
                if nameA in awards_authors:
                    listawards = awards_authors[nameA]
                    listawards.append(line.strip())
                else:
                    listawards = []
                    listawards.append(line.strip()
                    awards_authors[nameA] = listawards
        if similar:
            if line == '\n':
                similar = False
                name_of_author = True
            else:
                sim_author = line.split(', ')
                nameS = sim_author[1].strip() + ' ' + sim_author[0].strip()
                if nameA in similar_authors:
                    similar_list = similar_authors[nameA]
                    similar_list.append(nameS)
                else:
                    similar_list = []
                    similar_list.append(nameS)
                    similar_authors[nameA] = similar_list
                continue

This works great! 这很棒! However, if the text file contains an entry with just a name (ie no awards, and no similar authors), it screws the whole thing up, generating an IndexError: list index out of range at this part Zname = sim_author[1].strip()+" "+sim_author[0].strip() ) 但是,如果文本文件仅包含一个名称(即没有奖项,也没有类似的作者)的条目,则会使整个过程搞砸,从而在此部分生成IndexError: list index out of range Zname = sim_author[1].strip()+" "+sim_author[0].strip()

How can I fix this? 我怎样才能解决这个问题? Maybe with a 'try, except function' in that area? 也许在该区域进行了'try, except function'
Also, I wouldn't mind getting rid of those continue functions, I wasn't sure how else to keep it going. 而且,我不介意摆脱那些继续功能,我不确定如何继续进行下去。 I'm still pretty new to this, so any help would be much appreciated! 我对此还很陌生,因此任何帮助将不胜感激! I keep trying stuff and it changes another section I didn't want changed, so I figured I'd ask the experts. 我一直在尝试,它改变了另一个我不想更改的部分,所以我想请教专家。

How about doing it this way, just to get the data in, then manipulate the dictionary any ways you want. 这样进行操作,只是获取数据,然后以所需的任何方式操作字典。

test.txt contains your data test.txt包含您的数据

Brabudy, Ray
Hugo Award
Nebula Award
Saturn Award
Ellison, Harlan
Heinlein, Robert
Asimov, Isaac
Clarke, Arthur

Ellison, Harlan
Nebula Award
Hugo Award
Locus Award
Stephenson, Neil
Vonnegut, Kurt
Morgan, Richard
Adams, Douglas

And my code to parse it. 和我的代码来解析它。

award_parse.py award_parse.py

data = {}
name = ""
awards = []

f = open("test.txt")

for l in f:
    # make sure the line is not blank don't process blank lines
    if not l.strip() == "":

        # if this is a name and we're not already working on an author then set the author
        # otherwise treat this as a new author and set the existing author to a key in the dictionary
        if "," in l and len(name) == 0:
            name = l.strip()

        elif "," in l and len(name) > 0:
            # check to see if recipient is already in list, add to end of existing list if he/she already
            # exists.
            if not name.strip() in data:
                data[name] = awards
            else:
                data[name].extend(awards)

            name = l.strip()
            awards = []

        # process any lines that are not blank, and do not have a ,
        else:
            awards.append(l.strip())


f.close()


for k, v in data.items():
    print("%s got the following awards: %s" % (k,v))

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

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