简体   繁体   中英

PYTHON: searching for a word in a text file which includes spaces in between words

I'm stuck on how I would go about searching for a book title in a text file because the titles has spaces in between them.

This is the text file im trying to search:

#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

My code:

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

Anyone know how to do this?

You can use the split function to remove the spaces:

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

Fix the indentation in the code before running it, or it will give an error.

If your file is csv then:

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")

Note: This will only work if your file is csv

You can read your file into a python list so that each time you want to find a single book name or author from the file, you don't have to reload the file again and again.

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

This makes your code fast, if the file is large, Now: you can simply find what you want to find:

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")

Note the use of f-string !

Here you go:

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM