首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从文本文件创建词典时出错

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

Stack Overflow用户
提问于 2017-12-08 06:07:53
回答 1查看 58关注 0票数 0

我一直在开发一个函数,它将从一个开放的文本文件中更新两个字典(类似的作者和他们赢得的奖项)。文本文件如下所示:

代码语言:javascript
复制
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

诸若此类。名字是作者的名字(姓在前,名在后),然后是他们可能赢得的奖项,然后是与他们相似的作者。这是我到目前为止所得到的:

代码语言:javascript
复制
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

这个很好用!但是,如果文本文件包含的条目只有一个名称(即没有奖项,也没有类似的作者),它就会搞砸整个过程,在这部分Zname = sim_author[1].strip()+" "+sim_author[0].strip()中生成一个IndexError: list index out of range )

我该如何解决这个问题呢?也许在那个区域有一台'try, except function'

此外,我也不介意去掉那些continue函数,我不确定如何让它继续运行。我对此还是个新手,所以任何帮助都将不胜感激!我一直在尝试一些东西,它改变了另一个我不想改变的部分,所以我想我应该请教专家。

EN

回答 1

Stack Overflow用户

发布于 2017-12-08 06:40:38

这样做如何,只是为了获得数据,然后以任何你想要的方式操作字典。

test.txt包含您的数据

代码语言:javascript
复制
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

代码语言:javascript
复制
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))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47704639

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档