In Python how to make each line in a file to be a tuple and make them in a list?
我们有一个功课,我有一个严重的问题。
关键是使每一行成为一个元组,并将这些元组设置为一个列表。
喜欢
此外,有许多字符串用逗号分隔,如
这是一个问题:
一本书可以表示为作者的姓氏,作者的firstName,标题,日期和ISBN的元组。
-
编写一个函数
readBook() ,给定一个包含此信息的逗号分隔字符串,返回表示该书的元组。 -
编写一个函数
readBooks() ,给定每个书中包含一个逗号分隔行的文本文件的名称,使用readBook() 返回元组列表,每个元组描述一本书。 -
编写一个函数
buildIndex() ,在给定readBooks() 返回的书籍列表的情况下,构建从关键词到书名的地图。关键词是书中标题中的任何单词,除了"a","an"或"the"。
这是我的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | RC=("Chann","Robbbin","Pride and Prejudice","2013","19960418") RB=("Benjamin","Franklin","The Death of a Robin Thickle","1725","4637284") def readBook(lastName, firstName, booktitle, date, isbn): booktuple=(lastName, firstName, booktitle, date, isbn) return booktuple # print readBook("Chen","Robert","Pride and Prejudice","2013","19960418") def readBooks(file1): inputFile = open(file1,"r") lines = inputFile.readlines() book = (lines) inputFile.close() return book print readBooks("book.txt") BooklistR=[RC,RB] def buildIndex(file2): inputFile= open("book.txt","r") Blist = inputFile.readlines() dictbooks={} for bookinfo in Blist: title=bookinfo[2].split() for infos in title: if infos.upper()=="A": title.remove(infos) elif infos.upper()=="THE": title.remove(infos) elif infos.upper()=="AN": title.remove(infos) else: pass dictbooks[tuple(title)]= bookinfo[2] return dictbooks print buildIndex("book.txt") #Queries# def lookupKeyword(keywords): dictbooks=buildIndex(BooklistR) keys=dictbooks.viewkeys() values=dictbooks.viewvalues() for keybook in list(keys): for keyw in keywords: for keyk in keybook: if keyw== keyk: printoo= dictbooks[keybook] else: pass return printoo print lookupKeyword("Robin") |
这样的事情有什么问题?:
1 2 | with open(someFile) as inputFile: myListofTuples = [tuple(line.split(',')) for line in inputFile.readlines()] |
[解释根据罗伯特的评论添加]
第一行在
您可以在以下网址上阅读有关丑陋细节的内容:Python Docs:Context Managers,但它的全部要点是我们打开someFile,并保证在代码执行完毕后它将被正确关闭(该套件)
在这种情况下,我们使用
我们的
列表理解是另一个相当高级的编程概念。有许多非常高级的语言以各种方式实现它们。在Python的情况下,它们可以追溯到比
因此,列表推导在Python代码中相当普遍,而
Python中的列表文字看起来像:
在我的例子中,我使用
这只是一种非常简洁的说法:
1 2 3 | myListofTuples = list() for line in inputfile.readlines(): myListofTuples.append(line.split(',')) |
一个可能的计划:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | import fileinput def readBook(str): l = str.split(',') t = (l[0:5]) return t #b = readBook("First,Last,Title,2013,ISBN") #print b def readBooks(file): l = [] for line in fileinput.input(file): t = readBook(line) # print t l.append(t) return l books = readBooks("data") #for t in books: # for f in t: # print f def buildIndex(books): i = {} for b in books: for w in b[2].split(): if w.lower() not in ('a', 'an', 'the'): if w not in i: i[w] = [] i[w].append(b[2]) return i index = buildIndex(books) for w in sorted(index): print"Word:", w for t in index[w]: print"Title:", t |
示例数据文件(代码中称为"数据"):
1 2 3 | Austen,Jane,Pride and Prejudice,1811,123456789012X Austen,Jane,Sense and Sensibility,1813,21234567892 Rice-Burroughs,Edgar,Tarzan and the Apes,1911,302912341234X |
样本输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | Word: Apes Title: Tarzan and the Apes Word: Prejudice Title: Pride and Prejudice Word: Pride Title: Pride and Prejudice Word: Sense Title: Sense and Sensibility Word: Sensibility Title: Sense and Sensibility Word: Tarzan Title: Tarzan and the Apes Word: and Title: Pride and Prejudice Title: Sense and Sensibility Title: Tarzan and the Apes |
请注意,由于嵌入了逗号,数据格式无法支持书籍标题,如"狮子,女巫和魔衣橱"。如果文件是CSV格式,并且字符串周围有引号,那么它可以管理它。
我不确定这是完全最低限度的Pythonic代码(完全不确定),但它确实符合要求。