We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,272 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Reading a part of text?python

Hi !
I have books.txt and it contain 20 books with author,the books.txt look like so..
Mark Lutz/learning python
James payane/Beginning Python
Noah gift/Python for Unix and Linux System Administration

If user search for book it just print book and author if user search for author print (“you cannot search for author here ,you should look for title books here!.”)

def main():
    search = input("which titel of book you are looking after? ")
    if search in open("books.txt").read():
        parts = search.split("/")
        for part in parts:
            print(parts, " Books found.")



main()
2
Contributors
3
Replies
2 Days
Discussion Span
11 Months Ago
Last Updated
5
Views
tony75
Junior Poster
155 posts since Mar 2011
Reputation Points: 20
Solved Threads: 4
Skill Endorsements: 0

You are not saving the books file which you read as one string at line 3 anywhere. Why are you splitting then input from user for title from '/'? Your example seems not allow '/' in title as it separates author from title.

pyTony
pyMod
Moderator
6,310 posts since Apr 2010
Reputation Points: 879
Solved Threads: 986
Skill Endorsements: 26

Thanks Tony
If I change may question!

if user search for a book title or author and found was True it will show like this
Mark Lutz/learning python
if not tittle of book not found

def main():

    search = input("which titel of book you are looking for? ")
    file = open("books.txt")

    if search in open("books.txt").read():
        print(search, "found")

main()

which titel of book you are looking for? learning python
learning python found

which titel of book you are looking for? Mark Lutz
Mark Lutz found

But I neaad the out put will be(book title and author)
Mark Lutz/learning python

tony75
Junior Poster
155 posts since Mar 2011
Reputation Points: 20
Solved Threads: 4
Skill Endorsements: 0

But I am not sure user can be able to give exactly same format, maybe only Family name of author, then wouldn't it be actually more usefull to show what there is in the line matching instead of echoing back the search string? So you should iterate the file line by line for matches.

Maybe you could compare your code with my example data file code snipet? http://www.daniweb.com/software-development/python/code/293490/text-file-based-information-access-by-field-name-and-number

pyTony
pyMod
Moderator
6,310 posts since Apr 2010
Reputation Points: 879
Solved Threads: 986
Skill Endorsements: 26

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0615 seconds using 2.66MB