Reputation: 35
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?
Upvotes: 1
Views: 242
Reputation: 34146
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")
Upvotes: 1