| | |
How to search a line word from text file in Python?
Thread Solved |
•
•
•
•
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?
Python Syntax (Toggle Plain Text)
log=open('1234txt','r') log_read=log.readlines() x="" for a in log_read: if (a=='you love me'): x="True"
that would work, but couldnt you use:
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
python Syntax (Toggle Plain Text)
log=open('1234txt','r') log_read=log.readlines() x="you love me" if x in log_read: 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
'it is better to fight for something than to live for nothing'General George S Patton
Or: if it doesn't neccessarily take up the entire line:
return_list would have all occurrences of x.
Python Syntax (Toggle Plain Text)
my_file = open('1234.txt','r') whole_file = my_file.read() my_file.close() x = 'you love me' def find_x_in_string(x,whole_file) index = 0 end = len(x) - 1 return_list = [] while 1: try: my_str = whole_file[index:end] if my_str == x: return_list.append(my_str) index += 1 end += 1 except IndexError: break return return_list 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.
![]() |
Similar Threads
- Counting Occurences of a Word in a Text File (C#)
- search in text file (C++)
- c++ find word from text file (C++)
- Need help!!!search word from text file and display lines which match the word. (C++)
- Storing Words from Text File in Array (C++)
Other Threads in the Python Forum
- Previous Thread: Please help in comparing numbers..!!
- Next Thread: subprocess module issues
| Thread Tools | Search this Thread |
address aliased anydbm bash beginner bits calling casino changecolor class clear conversion convert corners count cturtle cursor curves definedlines dictionary digital dynamic dynamically events examples excel external file float format frange function gui handling hints homework i/o iframe import info input java line linux list lists loan loop matching mouse multiple number numbers output parsing path port prime programming projects py py2exe pygame python random rational raw_input recursion recursive scrolledtext searchingfile shebang signal singleton string strings subprocess table tails terminal text thread threading time tkinter tlapse tooltip tuple tutorial type ubuntu unicode urllib urllib2 valueerror variable web-scrape whileloop word wxpython






