6 Replies - 495 Views - Last Post: 03 August 2013 - 10:12 AM Rate Topic: -----

#1 Inuinu172  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 17-July 13

Reading and checking for specific words in txt file [problem]

Posted 03 August 2013 - 02:41 AM

What im trying to do:
Read a file line by line and check for a certain word. If the word is found, print that the word was found. If the word is not found, print that the word was not found.

Problem im having:
I receive both print messages instead of one.

Code:

    if message.body.startswith("!check"):
     with open('data/name.txt', 'r') as inF:
      for line in inF:
        if "apple" in line:
          print("an apple was found")
        else:
         if not "apple" in line:
          print("No apple was found")



The text document looks like:

apple
banana
carrot
watermelon
human



Thanks in advance if anyone can help ^^/>

Is This A Good Question/Topic? 0
  • +

Replies To: Reading and checking for specific words in txt file [problem]

#2 DblAAssassin  Icon User is offline

  • D.I.C Regular

Reputation: 33
  • View blog
  • Posts: 259
  • Joined: 11-May 13

Re: Reading and checking for specific words in txt file [problem]

Posted 03 August 2013 - 06:36 AM

So the output looks something like this...?

Quote

an apple was found
No apple was found


Also which module are you using? I can't seem to find any message.body module, only message which is a Email Module. But I found this D.I.C. tutorial that may help.
Answer these questions, and I'll be glad to help!
Was This Post Helpful? 0
  • +
  • -

#3 Inuinu172  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 17-July 13

Re: Reading and checking for specific words in txt file [problem]

Posted 03 August 2013 - 07:29 AM

View PostDblAAssassin, on 03 August 2013 - 06:36 AM, said:

So the output looks something like this...?

Quote

an apple was found
No apple was found


Yes

View PostDblAAssassin, on 03 August 2013 - 06:36 AM, said:

Also which module are you using? I can't seem to find any message.body module, only message which is a Email Module. But I found this D.I.C. tutorial that may help.
Answer these questions, and I'll be glad to help!


The message.body comes from the ch.py found on github.
Me and a few friends are working on a small project using it.
It connects to out chat room. When the user uses the command "!check", the program should check the file for the word "apple" in the file "name.txt" and prints the result.
Was This Post Helpful? 0
  • +
  • -

#4 MATTtheSEAHAWK  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 137
  • View blog
  • Posts: 782
  • Joined: 11-September 10

Re: Reading and checking for specific words in txt file [problem]

Posted 03 August 2013 - 08:52 AM

I can't exactly tell what you want from the question, but I'm guessing you only want to print one line of text. If you want to do that, you should just read the entire file and check if apple is in the file. You can do that like so:

with open('data/name.txt', 'r') as file:
    if "apple" in file.read():
            print("found")
    else:
            print("not found")



Is that what you're going for?
Was This Post Helpful? 1
  • +
  • -

#5 Inuinu172  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 17-July 13

Re: Reading and checking for specific words in txt file [problem]

Posted 03 August 2013 - 09:28 AM

View PostMATTtheSEAHAWK, on 03 August 2013 - 08:52 AM, said:

I can't exactly tell what you want from the question, but I'm guessing you only want to print one line of text. If you want to do that, you should just read the entire file and check if apple is in the file. You can do that like so:

with open('data/name.txt', 'r') as file:
    if "apple" in file.read():
            print("found")
    else:
            print("not found")



Is that what you're going for?


Thanks man!
This worked exactly how I was wanting it to.
I think the problem was I was checking each line, and printing the result.
If the entire file is read, it should only print the result once.
And it seems to be working fine.
Thanks again!

- Inuinu

End result:


Connected to room
Users: 1
NOTE: tryhardhusky joined the chat
Users: 2
tryhardhusky: !check
an apple was found
tryhardhusky: !remove apple
apple removed from list
tryhardhusky !check
No apple was found

Was This Post Helpful? 0
  • +
  • -

#6 MATTtheSEAHAWK  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 137
  • View blog
  • Posts: 782
  • Joined: 11-September 10

Re: Reading and checking for specific words in txt file [problem]

Posted 03 August 2013 - 09:32 AM

View PostInuinu172, on 03 August 2013 - 12:28 PM, said:

View PostMATTtheSEAHAWK, on 03 August 2013 - 08:52 AM, said:

I can't exactly tell what you want from the question, but I'm guessing you only want to print one line of text. If you want to do that, you should just read the entire file and check if apple is in the file. You can do that like so:

with open('data/name.txt', 'r') as file:
    if "apple" in file.read():
            print("found")
    else:
            print("not found")



Is that what you're going for?


Thanks man!
This worked exactly how I was wanting it to.
I think the problem was I was checking each line, and printing the result.
If the entire file is read, it should only print the result once.
And it seems to be working fine.
Thanks again!

- Inuinu

End result:


Connected to room
Users: 1
NOTE: tryhardhusky joined the chat
Users: 2
tryhardhusky: !check
an apple was found
tryhardhusky: !remove apple
apple removed from list
tryhardhusky !check
No apple was found


You're welcome! That was your problem. You also had one more if statement than you needed when you were checking to print "not found." Glad it worked out :)
Was This Post Helpful? 1
  • +
  • -

#7 DblAAssassin  Icon User is offline

  • D.I.C Regular

Reputation: 33
  • View blog
  • Posts: 259
  • Joined: 11-May 13

Re: Reading and checking for specific words in txt file [problem]

Posted 03 August 2013 - 10:12 AM

View PostInuinu172, on 03 August 2013 - 09:28 AM, said:

View PostMATTtheSEAHAWK, on 03 August 2013 - 08:52 AM, said:

I can't exactly tell what you want from the question, but I'm guessing you only want to print one line of text. If you want to do that, you should just read the entire file and check if apple is in the file. You can do that like so:

with open('data/name.txt', 'r') as file:
    if "apple" in file.read():
            print("found")
    else:
            print("not found")



Is that what you're going for?


Thanks man!
This worked exactly how I was wanting it to.
I think the problem was I was checking each line, and printing the result.
If the entire file is read, it should only print the result once.
And it seems to be working fine.
Thanks again!

- Inuinu

End result:


Connected to room
Users: 1
NOTE: tryhardhusky joined the chat
Users: 2
tryhardhusky: !check
an apple was found
tryhardhusky: !remove apple
apple removed from list
tryhardhusky !check
No apple was found

Yes your correct it was reading the file line by line, and printing if apple was on that line.

This post has been edited by DblAAssassin: 03 August 2013 - 10:13 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1