How to search a line word from text file in Python?

Thread Solved

Join Date: Oct 2007
Posts: 65
Reputation: denniskhor is an unknown quantity at this point 
Solved Threads: 0
denniskhor denniskhor is offline Offline
Junior Poster in Training

How to search a line word from text file in Python?

 
0
  #1
Jul 13th, 2009
My question is :

x = "you love me"

Inside 1234.txt file:

i love you
you love me
we love you

If i wan IF x == "you love me" from 1234.txt, THEN x is true.

how to do it? any one can help?
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 231
Reputation: sravan953 has a little shameless behaviour in the past 
Solved Threads: 28
sravan953's Avatar
sravan953 sravan953 is offline Offline
Posting Whiz in Training

Re: How to search a line word from text file in Python?

 
0
  #2
Jul 13th, 2009
Originally Posted by denniskhor View Post
My question is :

x = "you love me"

Inside 1234.txt file:

i love you
you love me
we love you

If i wan IF x == "you love me" from 1234.txt, THEN x is true.

how to do it? any one can help?
Try:

  1. log=open('1234txt','r')
  2. log_read=log.readlines()
  3.  
  4. x=""
  5. for a in log_read:
  6. if (a=='you love me'):
  7. x="True"
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 396
Reputation: leegeorg07 is an unknown quantity at this point 
Solved Threads: 31
leegeorg07's Avatar
leegeorg07 leegeorg07 is offline Offline
Posting Whiz

Re: How to search a line word from text file in Python?

 
0
  #3
Jul 13th, 2009
that would work, but couldnt you use:
  1. log=open('1234txt','r')
  2. log_read=log.readlines()
  3.  
  4. x="you love me"
  5. if x in log_read:
  6. return True

i haven't tested this but it should work up to python 3/3.1
i dont know if it still supports the 'in' in it but its worth a shot
i use it all the time with 25
don't judge me because I'm a year 8!

'it is better to fight for something than to live for nothing'General George S Patton
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,542
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 173
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: How to search a line word from text file in Python?

 
0
  #4
Jul 13th, 2009
If you are specifically looking for the text line:
'you love me'
then sravan953 code works best.

leegeorg07 code using 'in' would also give you a positive with a line like:
'you love me not'
which may be misleading.
Last edited by Ene Uran; Jul 13th, 2009 at 11:23 am.
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,279
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 176
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: How to search a line word from text file in Python?

 
0
  #5
Jul 13th, 2009
On the other hand 'in' would fetch:
'you love me!'
which would be a shame to miss!
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 106
Reputation: zachabesh is an unknown quantity at this point 
Solved Threads: 16
zachabesh's Avatar
zachabesh zachabesh is offline Offline
Junior Poster

Re: How to search a line word from text file in Python?

 
0
  #6
Jul 13th, 2009
Or: if it doesn't neccessarily take up the entire line:

  1.  
  2. my_file = open('1234.txt','r')
  3. whole_file = my_file.read()
  4. my_file.close()
  5.  
  6. x = 'you love me'
  7.  
  8.  
  9. def find_x_in_string(x,whole_file)
  10. index = 0
  11. end = len(x) - 1
  12. return_list = []
  13. while 1:
  14. try:
  15. my_str = whole_file[index:end]
  16. if my_str == x:
  17. return_list.append(my_str)
  18. index += 1
  19. end += 1
  20. except IndexError:
  21. break
  22. return return_list
  23.  
  24. return_list = find_x_in_string(x,whole_file)

return_list would have all occurrences of x.
Last edited by zachabesh; Jul 13th, 2009 at 3:37 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC