Replacing line in a text file

Frederic Rentsch anthra.norell at vtxmail.ch
Fri Sep 22 16:45:13 EDT 2006


CSUIDL PROGRAMMEr wrote:
> Folks
> I am trying to read a file
> This file has a line containing string  'disable = yes'
>
> I want to change this line to 'disable = no'
>
> The concern here is that , i plan to take into account the white spaces
> also.
>
> I tried copying all file int list and then tried to manipulate that
> list
>
> But the search is not working
>
> Any answer
>
> thanks
>
>   
>>> s = '''Folks! I am trying to read a file
This file has a line containing string  'disable = yes'
I want to change this line to 'disable = no' ...'''

The second line is the one to change. Okay?

>>> import SE
>>> Translator = SE.SE ('"disable \= yes=disable \= no"')
>>> print Translator (s)
Folks! I am trying to read a file
This file has a line containing string  'disable = no'
I want to change this line to 'disable = no' ...

Did it! - I don't know if it 'takes into account the white spaces' as I don't exactly understand what you mean by that. If need be, just change the substitution definition that makes the Translator to suit your needs. In an IDLE window you can work trial-and-error style five seconds per try. If you want to do other translations, just add more substitution definitions, as many as you want. It will do files too. No need to read them. Like this:

>>> Translator ('file_name', 'translated_file_name')

If this approach seems suitable, you'll find SE here: http://cheeseshop.python.org/pypi/SE/2.2%20beta


Regards

Frederic




More information about the Python-list mailing list