【发布时间】:2020-12-05 14:47:03
【问题描述】:
我不知道如何在文本文件中搜索书名,因为书名之间有空格。
这是我试图搜索的文本文件:
#Listing showing sample book details
#AUTHOR, TITLE, FORMAT, PUBLISHER, COST?, STOCK, GENRE
P.G. Wodehouse, Right Ho Jeeves, hb, Penguin, 10.99, 5, fiction
A. Pais, Subtle is the Lord, pb, OUP, 12.99, 2, biography
A. Calaprice, The Quotable Einstein, pb, PUP, 7.99, 6, science
M. Faraday, The Chemical History of a Candle, pb, Cherokee, 5.99, 1, science
C. Smith, Energy and Empire, hb, CUP, 60, 1, science
J. Herschel, Popular Lectures, hb, CUP, 25, 1, science
C.S. Lewis, The Screwtape Letters, pb, Fount, 6.99, 16, religion
J.R.R. Tolkein, The Hobbit, pb, Harper Collins, 7.99, 12, fiction
C.S. Lewis, The Four Loves, pb, Fount, 6.99, 7, religion
E. Heisenberg, Inner Exile, hb, Birkhauser, 24.95, 1, biography
G.G. Stokes, Natural Theology, hb, Black, 30, 1, religion
我的代码:
desc = input('Enter the title of the book you would like to search for: ')
for bookrecord in book_list:
if desc in bookrecord:
print('Book found')
else:
print('Book not found')
break
有人知道怎么做吗?
【问题讨论】:
-
"im" 不是一个词。在提交您的帖子之前,请花一点时间使用正确的拼写。
-
它的
csv文件,通过import csv你可以使用csv.reader,它会用“,”分割你的行。 -
您能否提供一个在此问题中使用 csv 的示例
-
@JJBANG458 如果您发现有用的帖子,请点赞并打勾