I want to find username and replace the value with another value. I
don't know what the username value in advance though. How to do it in
python?
Could be done using regular expressions. Ususally for such tasks one
would prefer pythons string manipulation functions, but if you want to
preserve whitespace, I think a rex is in order here:
import re, sys
new_name = "foobar"
rex = re.compile(r'(^.*username *=[^"]*")([^"]*)(".*$)')
for line in sys.stdin:
m = rex.match(line)
if m is not None:
line = "%s%s%s\n" % (m.group(1), new_name, m.group(3))
sys.stdout.write(line)
use with
python script.py < config_in > config_out
Regards,
Diez
[service_A]
dir=C:\test
username=test
password=test
Then you can do the following to change:
import ConfigParser
INI=ConfigParser.ConfigParser()
INI.read(r'C:\program.ini')
INI.set('service_A','username','newusername')
fp=open(r'c:\program.ini','w')
INI.write(fp)
fp.close()
Larry Bates
Your config file looks like a cut-down mutated version of XML. Maybe you
should use real XML and the tools for working with it?
Or perhaps you should use module ConfigParser? Despite the temptation to
reinvent the wheel, it is rarely a good idea.
If you have already read the config file into a nested dictionary, then
all you need to do is:
py> service_A = ReadConfigFile() # you must write this function
py> print service_A["params"]
{'dir': 'c:\test', 'username': 'test', 'password': 'test'}
py> service_A['params']['username'] = 'J. Random User'
py> WriteConfigFile(service_A)
Or if you insist on treating the config file as a string, try something
like this:
py> s = open('filename', 'r').read()
py> print s
service A = {
params {
dir = "c:\test",
username = "test",
password = "test"
}
}
py> target = 'username = '
py> p1 = s.find(target) + len(target)
py> p2 = s.find(',\n', p1)
py> s = s[:p1] '"J. Random User"' + s[p2:]
That's not good enough for professional code, but it will get you started.
--
Steven.
>>> import re
>>> s = """service A = {
params {
dir = "c:\test",
username = "test",
password = "test"
}
}"""
>>> usr_str = r'username = ".*",'
>>> usr_reg = re.compile(usr_str)
>>> usr_reg.sub('username = "%s",' % ('new_usr'),s)
'service A = {\n params {\n dir = "c:\test",\n username =
"new_usr",\n password = "test"\n }\n\n}'
A XML file or ConfigParser is appealing but this is part of a legacy
system...:(
> So how to overwrite the config file directly in script.py instead of
> running script.py with two params?
Don't overwrite the file directly. Save a copy, then rename it. That
way, you don't replace the new version until you know the old version
is safely written to disk, thus avoid accidently losing the data in
the file.
def modfile(filein, mod):
fileout = filein + ' temp'
fin = open(filein, 'r')
fout = open(fileout, 'w')
fout.write(mod(fin.read()))
fin.close()
fout.close()
os.unlink(filein)
os.rename(fileout, filein)
The truly paranoid will replace os.unlink(filein) with
os.rename(filein, filein + '.back').
<mike
--
Mike Meyer <m...@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
But there is an extra line break after each line in FILE. Why is that?
I tried to line = "%s%s%s" % (m.group(1), new_name, m.group(3)) but it
turns out there are two extra line breaks after each line...
As yoyu can see, iterating over a (text) file gives you lines with a
line ending. Print adds a line ending as well. Instead use
import sys
...
...
...
sys.stdout.write(l)
You will find you lose the blank lines.
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/
> Now it works:
> rex = re.compile(r'(^.*username *=[^"]*")([^"]*)(".*$)')
> for line in fileinput.input(FILE, inplace=1):
> m = rex.match(line)
> if m is not None:
> line = "%s%s%s\n" % (m.group(1), new_name, m.group(3))
> print line
>
> But there is an extra line break after each line in FILE. Why is that?
Because print prints a newline after all the values you pass it.
> I tried to line = "%s%s%s" % (m.group(1), new_name, m.group(3)) but it
> turns out there are two extra line breaks after each line...
It's not clear what you mean by this. I would have expected this
change to solve the problem. Where do you get two extra newlines?
> hm...Is there a way to get rid of the newline in "print"?
Yes, by using another language *wink*
Or, instead of using print, use sys.stdout.write().
--
Steven.
> "us...@yahoo.com" <us...@yahoo.com> writes:
>
> > So how to overwrite the config file directly in script.py instead of
> > running script.py with two params?
>
> Don't overwrite the file directly. Save a copy, then rename it. That
See module fileinput in the standard library, it does this for you
automatically when used in the right way -- much better than rolling
your own and having to debug it and maintain it forever!
Alex
> On Fri, 28 Oct 2005 12:27:36 -0700, us...@yahoo.com wrote:
>
> > hm...Is there a way to get rid of the newline in "print"?
>
> Yes, by using another language *wink*
Ending the print statement with a comma also works;-)
> Or, instead of using print, use sys.stdout.write().
Yep, generally better than 'print'.
Alex