Newbie question: string replace

Mike Meyer mwm at mired.org
Fri Oct 28 15:09:37 EDT 2005


"usgog at yahoo.com" <usgog at yahoo.com> writes:

> Now it works:
> rex = re.compile(r'(^.*username *=[^"]*")([^"]*)(".*$)')
> for line in fileinput.input(FILE, inplace=1):
>      m = rex.match(line)
>      if m is not None:
>          line = "%s%s%s\n" % (m.group(1), new_name, m.group(3))
>      print line
>
> But there is an extra line break after each line in FILE. Why is that?

Because print prints a newline after all the values you pass it.

> I tried to line = "%s%s%s" % (m.group(1), new_name, m.group(3)) but it
> turns out there are two extra line breaks after each line...

It's not clear what you mean by this. I would have expected this
change to solve the problem. Where do you get two extra newlines?

       <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list