1

I'm trying to search for specific word using python

f = open("C:\\Users\\Suleiman JK\\Desktop\\keyword.txt")
keyword = f.readlines() -----> keyword[0] = "obj"
file = open ("C:\\Users\\Suleiman JK\\Desktop\\Hello.pdf")
text = file.readlines()


for line in text:
    if re.search (r"\b"+keyword[0]+r"\b"):
        print (line)

it doesn't give me the word I'm looking for

but when I use This it works fine:

for line in text:                                    
    if re.search (r"\b"+"obj"+r"\b"):
        print (line)

or when I use this it gives me "obj" and "endobj":

for line in text:                                    
    if re.search (keyword[0]):
        print (line)

could any one help me?

1
  • 2
    Are you sure keyword[0] is not "obj\n"? Try printing repr(keyword[0]). Commented Mar 25, 2014 at 20:21

1 Answer 1

1

What happens is that the in reality, the string is:

"obj\n"

the character "\n" is called a new-line character, and it's used to separate lines.

How to "delete" it?

You can use the method rstrip() from strings. This method will return a copy of the string with trailing characters removed. By default it will remove all whitespaces:

keyword[0].rstrip()

So, in your case, you can use it like:

re.search (r"\b" + keyword[0].rstrip() + r"\b")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.