![](/img/trans.png)
[英]Python: Need help creating dictionaries from text files and splitting a list
[英]Error when creating dictionaries from text files
我一直在研究一個功能,該功能將從一個打開的文本文件中更新兩個詞典(相似的作者,以及他們所獲得的獎項)。 文本文件如下所示:
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'
?
而且,我不介意擺脫那些繼續功能,我不確定如何繼續進行下去。 我對此還很陌生,因此任何幫助將不勝感激! 我一直在嘗試,它改變了另一個我不想更改的部分,所以我想請教專家。
這樣進行操作,只是獲取數據,然后以所需的任何方式操作字典。
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))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.