What this program is does is search the text and where the word appear it displays the line. However.
I am recieveing a SyntaxError: EOF while scanning triple-quated string literals. When i try to run in on python 2.6.
I dont have a an option of changeing versions. Just need to fix this error.
Any help will the greatly appreciated

# finding a search word in a text file using regex module re

import re

search = 'work'
# also matches 'like!', but not 'likely'
pattern = r"\b%s\b" % search
#print(pattern)  # for testing only --> \blike\b
rc = re.compile(pattern, re.IGNORECASE)

test_text = """\
1.	Principle of Segmentation:
   	a) Divide an object into independent parts
                 - Replace a large truck by a truck and trailer.
                 - Use a work breakdown structure for a large project.
                 - Sequential novel turn into movies
	b) Make an object easy to disassemble.
		 - Bicycle disassembling (saddle, wheels)
		 - Furniture (sofas, table)
		 - Crib
        c) Increase the degree of fragmentation or segmentation.
                 - Replace solid shades with Venetian blinds
                 - Computer covers with holes for ventilation
		 - Multi blade cartridge Razor


4.	Principle of Asymmetry:
        a.	If an object is symmetrical, change its shape to irregular.
                -Asymmetric paddles mix 
                -cement 
                -truck 
                -blender
                -cake mixer

"""

fname = "SearchFile.txt"
# save the test text  to a file
fout = open(fname, "w")
fout.write(test_text)
fout.close()

# read the file back line by line
word_list = []
for line in open(fname):
    match = rc.search(line)
    if match:
        print(match.group(0))  # for test
        print(line)

Recommended Answers

All 2 Replies

Works here in Python 2.6.6.

Output:

work
                 - Use a work breakdown structure for a large project.

Of course you do not need re if you do not want to, for example for simple searches of given exact word:

# finding a search word 
fname = "SearchFile.txt"
word = 'to'
for line in open(fname):
    if word in (''.join(c for c in word if c.isalpha()) for word in line.lower().split()):
        print line

I get the same answer in 2.7
However i have been working on android and trying to fix the code so it runs of the mobile phone. But that error keeps hindering me.

What i am trying to do is not open .txt file and read from there. But search from inside the code and give the result.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.