tags:

views:

45

answers:

1

i have tried startswith , find , re.sub but all seems to 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.

A: 

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.

retracile
Is double quotation mandotary can we use single quote for string match as i have line which have double quotes and string inside it and i want to use that in the string search
kdev
Single quotes work fine too.
retracile
Did I guess correctly on what you were wanting to accomplish?
retracile
Thanks for your help retracile. Iam sort of new to Jython and learning to write some added functionality for an Oracle Tool.I have a string some what like this <Field name="TableName" type="java.lang.String"><![CDATA[SALES_PERSON]]></Field> so what i actually wanted to do is when i find one part of the above string till - sales person i wish to replace with [above_string + trim ( ] and ( ]]></Field> ) with )]]></Field> ie. adding an close bracket inshort making the final string to replace as <Field name="TableName" type="java.lang.String"><![CDATA[ TRIM(SALES_PERSON)]]></Field>
kdev
If you're still having trouble with that, I would recommend asking another question.
retracile