【问题标题】:PYTHON: searching for a word in a text file which includes spaces in between wordsPYTHON:在文本文件中搜索一个单词,其中包含单词之间的空格
【发布时间】: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 如果您发现有用的帖子,请点赞并打勾

标签: python list search


【解决方案1】:

您可以使用拆分功能删除空格:

handle = open("book list.txt")#Open a file handle of the given file
            
for lines in handle:#A variable 'lines' that will iterate through all the variables in the file
    words = lines.split(,)#splits the file text into separate words and removes extra spaces.(the comma tells it to split where there are commas)
        
desc = input('Enter the title of the book you would like to search for: ')
        
for bookrecord in words:
    if desc in bookrecord:
        print('Book found')
else:
    print('Book not found')
         break

在运行之前修复代码中的缩进,否则会报错。

【讨论】:

  • 我试过了,但是当搜索“霍比特人”时,它仍然输出未找到的书
【解决方案2】:

如果你的文件是 csv 那么:

import pandas as pd

inp = input("Enter the books name: ")

bk = pd.read_csv('book_list.csv')#enter your file name
for row in bk.iterrows():
    if inp in row:
        print("Book found")
    else:
        print("Book not found")

注意:这仅在您的文件是 csv 时才有效

【讨论】:

  • 有没有更简单的方法可以做到这一点,因为我不能使用 pandas 进行评估
  • 我不确定是否有办法打开没有任何模块的 csv 文件...
【解决方案3】:

您可以将文件读入 python 列表,这样每次您想从文件中查找单个书名或作者时,都不必一次又一次地重新加载文件。

with open("file", "r") as file:
    data = file.readlines()

如果文件很大,这会使您的代码更快!现在,您可以简单地找到您想要查找的内容:

text_to_find = "C. Smith"

for idx, line in enumerate(data):
    if text_to_find in line:
        print(f"{text_to_find} FOUND AT {idx} LINE")

注意f-string的使用!

【讨论】:

    【解决方案4】:

    给你:

    def writeData(data):
        
        with open('file.txt', 'a') as w:
            w.write(data)
    
    def searchData(title):
        data = ''
        title_list = []
        with open('file.txt') as r:
            data = r.read()
        
        title_list = data.split(', ')[1::6]
        print(title_list)
        
        stock = data.split(', ')[5::6]
    
        print(stock)
        
        if title in title_list:
            print('Book Found')
    
        else:
            print('Book Not Found')
    
    
    writeData('P.G. Wodehouse, Right Ho Jeeves, hb, Penguin, 10.99, 5, fiction')
    writeData('A. Pais, Subtle is the Lord, pb, OUP, 12.99, 2, biography')
    
    searchData('Right Ho Jeeves')
    

    【讨论】:

    • 只是出于好奇,您会在这段代码中的哪个位置要求用户输入。谢谢你的回答!!
    • @JJBANG458 添加 title = input() 并将标题放在 seardhData 函数调用中 + 如果您觉得有帮助,请点赞并打勾
    • 我做到了,而且成功了!!!我会投票并打勾。同样从那本书的标题中,我需要访问库存编号并提供添加更多库存的选项,如果您不介意伙计,您能帮忙
    • 是的,这有点用,当我搜索标题时,我不确定如何访问该特定标题的库存水平,然后添加一个选项来增加该标题的库存水平
    • 不幸的是我不能添加另一个问题,因为我对这个网站太陌生了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-24
    • 2021-02-13
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    相关资源
    最近更新 更多