Newbie question re: regsub

Alan Gauld alan.gauld at gssec.bt.co.uk
Mon Jul 3 10:04:06 EDT 2000


Robert Goto wrote:
> I am sure there is a very simple answer to this.
> >>> import regsub
> >>> fimo = open('fimo.txt')
> >>> while 1:
>       line = fimo.readline()
>       if not line: break
        newline = regsub.gsub('orange', 'red', line)
        print newlineline


> getting is both the original string and the changed string?!

What you are getting is gsub displaying its output because 
you haven't assigned it to anything and then you print the 
original string. Assign the result of gsub() to a variable 
then print that variable(as shown above). Note that because 
strings are immutable in Python(ie can't be changed) gsub 
*returns* the new string it does not(cannot!) change 'line'.

Alan g.

-- 
=================================================
This post represents the views of the author 
and does not necessarily accurately represent 
the views of BT.



More information about the Python-list mailing list