text manipulation

Martin Franklin mfranklin1 at gatwick.westerngeco.slb.com
Fri Dec 16 06:25:59 EST 2005


Martin Franklin wrote:
> Johhny wrote:
> 
>>Hello,
>>
>>I am trying to write a script in python (to replace a perl script with
>>limited functionality). Now I have some issues. Currently I am using
>>the perl to load the file then regex parse certain lines to remove
>>characters (uncomment lines and change variables). I would like to take
>>that into the python script. I have had a look at the module "string"
>>and I dont think its what Im looking for.
>>
>>Here is an example of some text I would like to manipulate
>>
>>#comment here
>>#user_defined_variable = no
>>#
>>
>>I would like to make that
>>
>>#comment here
>>user_defined_variable = yes
>>#
>>
>>With perl/sed Its very easy, However Im having issues to do it in
>>python. Any advice would be great.
>>
>>Regards,
>>
>>Johhny.
>>
> 
> 
> 
> forget regular expressions for this job... strings have methods in
> python so for example:
> 
> 
> for line in file:
>      if line.startswith("#user_defined_variable = no"):
>          line.replace("#user_defined_variable = no",
>              "user_defined_variable = yes")
> 
> ... continue processing file / writing out stuff as you go
> 
> 
> Cheers
> Martin
> 




whoops forgot string methods return the new string.....

for line in file:
      if line.startswith("#user_defined_variable = no"):
          line = line.replace("#user_defined_variable = no",
              "user_defined_variable = yes")




More information about the Python-list mailing list