[Tutor] Regex parsing and writing to file
Kent Johnson
kent37 at tds.net
Thu Oct 18 13:38:46 CEST 2007
jon vspython wrote:
> Hi,
> I tried this on a file:
>
> for line in fileinput.input("myfile",inplace=1):
> re.sub(r'LOOP',r'PRUEBALOOP',line)
I'm not sure why you got any output at all. Were you running this in a
shell that prints out loop values for you (e.g. IPython)?
re.sub returns the modified line. Try
import sys
sys.stdout.write(re.sub(r'LOOP',r'PRUEBALOOP',line))
I used stdout.write() instead of print because you already have the
needed line endings in place.
Kent
>
> and for lines like this:
>
> PT_WT_INIT: LOOP
>
> I got this:
>
> 'PT_WT_INIT: PRUEBALOOP\r\n'
>
> I also tried without raw strings:
>
> re.sub('LOOP','PRUEBALOOP',line)
>
> but I got the same result.
>
> Any hint?
> Thanks, Jon.
>
>
More information about the Tutor
mailing list