Replacing line in a text file

C

CSUIDL PROGRAMMEr

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
 
N

Neil Cerutti

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

More code, less talk.

Seriously, posting your code and any error messages or unexpected
results you're getting would be helpful. That is, until such time
as Guido finalizes PyESP.

import PyESP
e = ESP.mindread(CSUIDL, "r")
# etc..
 
T

Tim Chase

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'

Sounds like

sed -i 's/disable *= *yes/disable = no/' file.txt

would do what you want. It doesn't catch word boundaries, so if
you have something like "foodisable = yes", it will replace this
too. Additionally, it only expects spaces around the equal-sign,
so if you have tabs, you'd have to modify accordingly.

If it must be done in python,

import re
r = re.compile(r"(\bdisable\s*=\s*)yes\b")
outfile = file('out.txt', 'w')
for line in file('in.txt'):
outfile.write(r.sub(r'\1no', line))

Add the re.IGNORECASE option if so desired. This doesn't have
the cautions listed above for the sed version. Wreckless code!
The concern here is that , i plan to take into account the white spaces
also.

I'm not sure what you intend by this. Do you want to disregard
whitespace? Do you want to keep leading indentation?

The above python should *just* replace "yes" with "no" in the
above context, not touching space or anything of the like.

-tkc
 
T

Tim Chase

That is, until such time as Guido finalizes PyESP.
import PyESP
e = ESP.mindread(CSUIDL, "r")

Sounds like an extension to the __future__ module.

How many other languages have a time-machine built-in, that will
take the programmer into the future?

No apache mod_precognition.
Perl offers no "use Eventual::Solution".
C/C++ offers no "#include <someday/answer.h>".
Same goes for Java's lack of "import java.ball.crystal"

Yet another reason to be a python programmer. :)

-tkc
 
F

Frederic Rentsch

CSUIDL said:
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
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?
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:

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


Regards

Frederic
 
E

Eric

Tim said:
Sounds like an extension to the __future__ module.


How many other languages have a time-machine built-in, that will
take the programmer into the future?

No apache mod_precognition.
Perl offers no "use Eventual::Solution".
C/C++ offers no "#include <someday/answer.h>".
Same goes for Java's lack of "import java.ball.crystal"

Yet another reason to be a python programmer. :)

-tkc

Oh, that time machine of his...

The only thing I know of that compares is Vim 7.0's :later command.
 
E

Eric

CSUIDL said:
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

Is this a configuration file? Depending on what exactly is in the file,
you may want to either import it directly or parse it -- especially if
"disable = yes" appears more than once in the file.

Could you post an example of the file for us?
 
L

Larry Bates

CSUIDL said:
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
Something simple like (not tested):

fp=open(filename, 'r')
data=fp.read()
fp.close()
data='disable = yes'.join(data.split('disable = no',1))
fp=open(filename, 'w')
fp.write(data)
fp.close()

-Larry Bates
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,270
Latest member
TopCryptoTwitterChannels_

Latest Threads

Top