problem with string.lower function

Don Low mt at open2web.com
Mon Apr 28 14:22:07 EDT 2003


I'm learning Python from the "Core Python" book by Wesley Chun. In
chapter 3 is a script called fgrepwc.py, which I understand well enough.
At the end of the chapter, he asks the reader to modify the script so
that it performs a search in a case-insensitive manner.

Here's an excerpt from the script:

def filefind(word, filename):
	count = 0
	try:
		fh = open(filename, 'r')
	except:
		print filename, ":", sys.exc_info()

	allLines = fh.readlines()
	fh.close()

	for eachLine in allLines:
	if string.find(eachLine, word) > -1:
		count = count + 1
		print eachLine,

This is how I modified the script to make it search in a
case-insensitive manner:

def filefind(word, filename):
	count = 0
	try:
		fh = open(filename, 'r')
	except:
		print filename, ":", sys.exc_info()

	allLines = fh.readlines()
	fh.close()

	for eachLine in allLines:
	test = string.lower(eachLine) // change is here
	if string.find(test, word) > -1:
		count = count + 1
		print eachLine,

When I run the script, however, I get the following:

 File "2bin/./fgrepwc.py", line 37
 		 if string.find(eachLine, word) > -1:
	     ^
SyntaxError: invalid syntax

Note: I imported string so the problem isn't with a missing module.

Any help is greatly appreciated.

-- 
Thanks,

Mark




More information about the Python-list mailing list