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

时间:2017-12-07 22:07:53

标签: python python-3.x

我一直在研究一个功能,它将从一个打开的文本文件中更新两个词典(类似的作者,以及他们赢得的奖励)。文本文件如下所示:

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

等等。第一个名字是作者姓名(姓氏第一,名字最后),然后是他们可能赢得的奖励,然后是与他们相似的作者。这是我到目前为止所得到的:

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

这很棒!但是,如果文本文件包含一个只有一个名称的条目(即没有奖项,也没有相似的作者),则会将整个内容搞砸,在此部分生成IndexError: list index out of range Zname = sim_author[1].strip()+" "+sim_author[0].strip()

我该如何解决这个问题?也许在该区域有一个'try, except function'? 另外,我不介意摆脱那些继续功能,我不知道还有什么可以继续下去。我对此仍然很陌生,所以任何帮助都会非常感激!我一直在努力尝试改变另一个我不想改变的部分,所以我想我会问专家。

1 个答案:

答案 0 :(得分:1)

如何以这种方式进行,只是为了获取数据,然后以任何方式操纵字典。

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

我的代码要解析它。

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))