Re: String manipulation questions

2008-04-09 Thread 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String manipulation questions

2008-04-09 Thread Duncan Booth
goldtech <[EMAIL PROTECTED]> wrote:

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

Yes.

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

Just do the line.replace() and then test for a change.

Question 3 (which I'm sure you meant to ask really) ...
No, you shouldn't be using a while loop here. Use a for loop and the 
enumerate builtin:

   for counter, line in enumerate(fileIN):
   newline = line.replace(oldstring, newstring)
   if newline != line:
   print 'match at line', counter+1
   fileOUT.write(newline)
-- 
http://mail.python.org/mailman/listinfo/python-list