Newbie Question
Scott David Daniels
scott.daniels at acm.org
Tue Jun 20 00:17:48 EDT 2006
placid wrote:
> Saint Malo wrote:
>> Andrew Robert wrote:
>>> Saint Malo wrote:
>>> # Open a file for read
>>> file=open(r'test.txt','r')
>>>
>>> # Read each line in file
>>> for line in file
>>>
>>> #Search each line
>>> if "A" in line:
>>>
>>> print "I found A"
>>>
>>> file.close()
>
Here's a bit nobody has suggested yet:
A) use sets of words for easier checking.
...
search_words = set(["red", "brown"])
for line in file:
words = set(line.split())
if words & search_words:
for word in search_words:
if word in words:
if word == 'red':
print 'weasels rip my flesh'
else:
print word
...
--Scott David Daniels
scott.daniels at acm.org
More information about the Python-list
mailing list