By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,537 Members | 874 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,537 IT Pros & Developers. It's quick & easy.

Replacing line in a text file

P: n/a
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

Sep 22 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On 2006-09-22, CSUIDL PROGRAMMEr <sy*********@yahoo.comwrote:
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..

--
Neil Cerutti
Life is indeed precious, and I believe the death penalty helps
affirm this fact. --Edward Koch
Sep 22 '06 #2

P: n/a
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


Sep 22 '06 #3

P: n/a
That is, until such time as Guido finalizes PyESP.
>
import PyESP
e = ESP.mindread(CSUIDL, "r")
Sounds like an extension to the __future__ module.
>>from __future__ import answers
answers.answer(op.question)
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


Sep 22 '06 #4

P: n/a
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

Sep 22 '06 #5

P: n/a

Tim Chase wrote:
That is, until such time as Guido finalizes PyESP.

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

Sounds like an extension to the __future__ module.
>>from __future__ import answers
>>answers.answer(op.question)

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.

Sep 23 '06 #6

P: n/a

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
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?

Sep 23 '06 #7

P: n/a
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
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
Sep 25 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.