简体   繁体   中英

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

How can I fix this? Maybe with a 'try, except function' in that area?
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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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