2017-12-07 9 views
0

私は、開いているテキストファイルから2つの辞書(似た作者と勝利した賞)を更新する機能を開発しています。テキストファイルは次のようになります。テキストファイルから辞書を作成するときにエラーが発生する

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という文字が表示されます。

どうすればいいですか?おそらくその地域の'try, except function'と?
また、私はこれらの継続機能を取り除いても構いません。私はまだこれでかなり新しいので、どんな助けも非常に高く評価されるでしょう!私はものを試し続け、私が変更したくない別のセクションを変更するので、私は専門家に尋ねると思った。

+1

カンマが欠落ファイル内の空白行が/あります。だから 'sim_author'は空にすることも、内部に1つの項目しか持たせることもできません。それはループ内の賞の旗をリセットしません。 –

答えて

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)) 
関連する問題