Let me show you a very bad consequence of this...

a=open('file1.txt','rb').read()
b=re.sub('x',a,'x')
open('file2.txt','wb').write(b)

Now if file1.txt contains a \n or \" then file2.txt is not the same as 
file1.txt while it should be.
Massimo

________________________________________
From: Tim Chase [EMAIL PROTECTED]
Sent: Tuesday, October 16, 2007 1:20 PM
To: DiPierro, Massimo
Cc: python-list@python.org; Berthiaume, Andre
Subject: Re: re.sub

> Even stranger
>
>  >>> re.sub('a', '\\n','bab')
> 'b\nb'
>  >>> print re.sub('a', '\\n','bab')
> b
> b

That's to be expected.  When not using a print statement, the raw
evaluation prints the representation of the object.  In this
case, the representation is 'b\nb'.  When you use the print
statement, it actually prints the characters rather than their
representations.  No need to mess with re.sub() to get the behavior:

   >>> s = 'a\nb'
   >>> s
   'a\nb'
   >>> print s
   a
   b
   >>> print repr(s)
   'a\nb'

-tkc
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to