String manipulation questions

G

goldtech

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.
 
G

goldtech

snip...
for counter, line in enumerate(fileIN):
newline = line.replace(oldstring, newstring)
if newline != line:
print 'match at line', counter+1
fileOUT.write(newline)

"enumerate" - haven't seen that before. Nice!

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top