[Tutor] Regex parsing and writing to file

jon vspython jonvspython at gmail.com
Thu Oct 18 14:05:36 CEST 2007


I was not trying to get any output. I was trying to modify a source file in
place, I just want to replace file content text automatically. So I tried
first from python shell in linux. This is what I did:

[jon at pc-600713 aut]$ python
Python 2.4.3 (#1, Oct 23 2006, 14:19:47)
[GCC 4.1.1 20060525 (Red Hat 4.1.1-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import fileinput
>>> import re
>>> for line in fileinput.input("ets.AUT1",inplace=1):
...     re.sub('LOOP','PRUEBALOOP',line)
...
>>>

But for an entry like the following in file "ets.AUT1":

PT_WT_INIT:                   LOOP
                              TRANS          (EvtMaint,ETSL_MAINTENANCE)
                              TRANS          (EvtTout,ETSL8)
                              ELSE_SLEEP


This is what I got "ets.AUT1" content like this one:
'\r\n'
'PT_WT_INIT:                   PRUEBALOOP\r\n'
'                              TRANS
(EvtMaint,ETSL_MAINTENANCE)\r\n'
'                              TRANS          (EvtTout,ETSL8)\r\n'
'                              ELSE_SLEEP\r\n'
'\r\n'

The file I got as a result won't work. I just wan't the pattern replaced.
Do you know how could I do it?
Thanks again, Jon.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20071018/ef3c5ff9/attachment.htm 


More information about the Tutor mailing list