String manipulation questions

goldtech goldtech at
Wed Apr 9 15:15:02 CEST 2008


Replacing strings in a text (likely an XML) file. Some newbie

while line:
    if line.find(newstring) != -1:
        print 'match at line'+str(counter)
    newline = line.replace(oldstring, newstring)

Question1: The replace method - If a string does not have the target
replacement "newstring", then newline equals oldstring? Ie. oldstring
is not changed in any way? Seems to be what I observe but just want to
confirm this.

Question2:  I'm using "line.find(newstring) != -1..."  because I want
to print when a replacement happens. Does "line.replace..." report
indirectly somehow when it replaces?

P.S. I know I should be using XSLT to transform XML -  but the above
seems to work for small text changes.

More information about the Python-list mailing list