String manipulation questions

goldtech goldtech at worldpost.com
Wed Apr 9 09:15:02 EDT 2008


Hi,

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

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

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?
Thanks

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