0

I have a problem in reading the text files in python. Below will describe things that im doing and what im stuck at:

This part functions to read user input and store it inside the textfile bookdata.txt. This part have no error in it.

bookdata = open("bookdata.txt","a")
Author = input("Author's Name: ")
title = input("Book Title: ")
keyword = input("Book Keyword: ")
publisher = input("Publisher: ")
year = input("Year Publish: ")

# write user input into "bookdata.txt"
bookdata.write("Author's Name:    " + Author + "\n")
bookdata.write("Book Title:       " + title + "\n")
bookdata.write("Keyword:          " + keyword + "\n")
bookdata.write("Publisher:        " + publisher + "\n")
bookdata.write("Year Published:   " + year + "\n\n")

bookdata.close() #closes textfile "bookdata.txt"

My second objective is to read the user input from the textfile bookdata.txt. This is the problematic part. Here is my code:

print("************** Search By **************")
print("\t[1] Search By Author")
print("\t[2] Search by Title")
print("\t[3] Search by Keyword")
print("\t[4] Go back")
print("***************************************")
    
searchby = input("Enter Search By Option: ")
if searchby == "1":
   #search author
elif searchby == "2":
   #search title
elif searchby == "3":
   #search keyword
elif searchby == "4":

i dont know how to read user input. For example, If i search by author, all of the related user input (author, title, keyword, etc...) will show. Does anyone know any method for doing this? Thank you in advance.

4
  • What do you mean by "read user input"? Commented Aug 27, 2021 at 9:05
  • seems like this relates to: 1) regular expression re to find and return data with specific keyword and 2) pandas to manage/analyze the whole data (of course pandas can do the job of re in this case) Commented Aug 27, 2021 at 9:07
  • This looks suspiciously like one of the standard beginner exercises. Is one of your books Harry Potter by any chance? You'll find a good number of existing questions about that. Commented Aug 27, 2021 at 9:24
  • Read about "Python List Slicing" to better understand the answer. And accept the answer if it solves/helps in your issue. Commented Aug 27, 2021 at 9:41

1 Answer 1

1

In this scenario you can use a small trick to read the Author's name, Title, Keyword etc.

So your file has a structure like this

"Author's Name: " 0
"Book Title: "    1
"Book Keyword: "  2
"Publisher: "     3
"Year Publish: "  4
"Author's Name: " 5
"Book Title: "    6
"Book Keyword: "  7
"Publisher: "     8
"Year Publish: "  9
"Author's Name: " 10
"Book Title: "    11
"Book Keyword: "  12
"Publisher: "     13
"Year Publish: "  14

So to Read/Search all the Author's Name we have to read the lines starting from 0, then skip next 4 lines and read 5th line. And continue skipping next 4 lines to get the next Author's Name.

So in code to get all Author's Name from the file we can use


data = myfile.readlines() # Read all Lines from your txt file

authors = data[0::5] # Start from index 0 and and skip next 4 and get value

# Similarly for Titles
titles = data[1::5] # Start from index 1 and skip next 4 and get value

# Keywords
keywords = data[2::5]


# Now to search in authors
if input_author in authors:
    print("Yes Author Exists")

Sign up to request clarification or add additional context in comments.

5 Comments

A small error: his file does not have any quotes ", and after Year published there is 1 extra new line. But I like this answer, no external module needed.
Good point. It was an illustration. I'm leaving it to @seenchan to understand why he should replace data[0::5] with data[0::6] to cater for the newline after "Year published".
how do I output all of the searched book? like, if user search title: Harry Potter, it would output author, title, keyword, publisher, and year publish.
@seenchan to follow up @glory9211's answer, you can find index of that title in titles: idx=titles.index('some name'), then use this index to retrieve keyword, author, year ...
It's a simple pattern finding problem. Let's say you use Titles to find the book. To show all information you know that if Title is 2nd field you have to read one line above and 3 lines below.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.