what is the correct command to search for an exact string ,
i have tried startswith , find , re.sub but all seems t开发者_JAVA百科o be find that string that matches part of it . iam looking for command which searchs the exact string which iam searching for in a given file.
Thanks for your help.
I'm guessing as to what you're asking about, but I think what you're wanting is to find an exact line of text in a file. If that's the case, there are a couple of ways to do it.
Use a regex to match start and end of line:
regex = re.compile("^whatevermystringis$", re.M)
match = regex.search(open(filename).read())
match.span()
Find the line using straight Python:
line_to_find = "blah, blah, blah\n"
for line_num, line in enumerate(open(filename)):
if line == line_to_find:
print line_num
(Take care in handling your line endings.)
Again, this is a guess as to what you're trying to ask.
精彩评论