通过python编辑配置文件 [英] Edit configuration file through python
问题描述
我需要通过python编辑配置文件,我试图搜索stackoverflow和谷歌,他们不包括我的情况,因为我需要替换文件中的行,并在我的搜索执行匹配。 b
$ b
另外,我发现如何做到这一行,我将执行至少8行替换文件中,我想知道是否有更清洁,更优雅这样做的方法比把10条线替换(foo,bar)线。
我需要像ENABLEPRINTER,PRINTERLIST,PRNT1港口。
我想匹配这个文本,并忽略任何后面的(例如:= PRNT1,PRNT2)。
所以我会做类似于
$ b $
replace('ENABLEPRINTER','y')
替换('PRINTERLIST','PRNT3)
该文件如下所示:
ENABLEPRINTER = n
PRINTERLIST = PRNT1,PRNT2
PRNT1.PORT = 9600
PRNT1.BITS = 8
还要注意这些文件大概有100行,我需要编辑大约10个。
谢谢非常感谢您的帮助。
更新:
使用@ J.F发布的代码。 Sebastian,我现在得到以下错误:
configobj.ParseError:解析第611行的值的错误。
第611行的文件是:
pre > log4j.appender.dailyRollingFile.DatePattern ='。'yyyy-MM-d
<所以问题是与'字符。
如果我注释掉该行,该脚本正常工作与@JF张贴的代码Sebastian。
import re
pat = re.compile('ENABLEPRINTER | PRINTERLIST | PRNT1.PORT')
$ b $ def jojo(mat,dic = {'ENABLEPRINTER':'y',
'PRINTERLIST':'PRNT3',
'PRNT1 ('configfile','rb +')作为f:
的内容返回dic [mat.group()]
= f.read()
f.seek(0,0)
f.write(pat.sub(jojo,content))
f.truncate()
之前:
ENABLEPRINTER = n
PRINTERLIST = PRNT1,PRNT2
PRNT1.PORT = 9600
PRNT1.BITS = 8
之后:
y = n
PRNT3 == PRNT1, PRNT2
734 = 9600
PRNT1.BITS = 8
太简单了,是明确的。说什么是错误或弱点。
正则表达式的优点是它们可以很容易地调整到特定的情况。
编辑:
我刚刚看到:
我想要做的是给变量赋一个新的值
!
您可以在之前/之后给出一个文件的例子。
。
编辑2
以下是更改文件中某些变量值的代码:
从os导入re
导入fsync
def更新(文件名,dico):
RE ='(('+'|'.join(dico.keys())+')\s * =)[^ \r\\\
] *?(\ r?\\\
| \r )'
pat = re.compile(RE)
$ b $ def jojo(mat,dic = dico):
return dic [mat.group(2)]。join .group(1,3))
打开(filename,'rb')作为f:
content = f.read ()
打开(文件名,'wb')作为f:
f.write(pat.sub(jojo,content))
#-------------------------------------------- ---------------
vars = ['ENABLEPRINTER','PRINTERLIST','PRNT1.PORT']
new_values = ['y' ,'PRNT3','8310']
what_to_change = dict(zip(vars,new_values))
更新('configfile_1.txt',what_to_change)
之前:
ENABLEPRINTER = n
PRINTERLIST = PRNT1,PRNT2
PRNT1.PORT = 9600
PRNT1.BITS = 8
$ b 之后:
pre $ E $ $ $ E $ $ $ $ $ $ PRINTERLIST = PRNT3
PRNT1.PORT = 8310
PRNT1.BITS = 8
I need to edit a configuration file through python and i tried searching on stackoverflow and google and they don't cover my situation, since i need to replace lines in the file and perform matches in my search.
Also, what i found covers how to do it for one line, i will be performing at least 8 line replacements in the file and I would like to know if there is a cleaner and more elegant way of doing this than putting 10 replace(foo, bar) lines altogether.
I need to "match" lines like "ENABLEPRINTER", "PRINTERLIST", "PRNT1.PORT". I want to match thesse text and ignore whatever follows (ex: "=PRNT1, PRNT2").
So i would do something like
replace('ENABLEPRINTER', 'y')
replace('PRINTERLIST', 'PRNT3)
The file looks like this:
ENABLEPRINTER=n
PRINTERLIST=PRNT1, PRNT2
PRNT1.PORT=9600
PRNT1.BITS=8
Also note these files are about 100 lines and i need to edit about 10 of them.
Thank you very much for your help.
UPDATE:
Using the code posted by @J.F. Sebastian, i'm now getting the following error:
configobj.ParseError: Parse error in value at line 611.
Line 611 of the file is:
log4j.appender.dailyRollingFile.DatePattern='.'yyyy-MM-d
So the problem is with the ' character.
If I comment out that line, the script is working fine with the code posted by @J.F. Sebastian.
import re
pat = re.compile('ENABLEPRINTER|PRINTERLIST|PRNT1.PORT')
def jojo(mat,dic = {'ENABLEPRINTER':'y',
'PRINTERLIST':'PRNT3',
'PRNT1.PORT':'734'} ):
return dic[mat.group()]
with open('configfile','rb+') as f:
content = f.read()
f.seek(0,0)
f.write(pat.sub(jojo,content))
f.truncate()
Before:
ENABLEPRINTER=n
PRINTERLIST=PRNT1, PRNT2
PRNT1.PORT=9600
PRNT1.BITS=8
After:
y=n
PRNT3==PRNT1, PRNT2
734=9600
PRNT1.BITS=8
Too simple to be definitive. Say what are the errors or weaknesses.
The advantage of regexes is they can be modulated easily to particular cases.
.
EDIT:
I've just seen that:
"what i want to do is assign a new value to the variable "
you could inform of that earlier !
Could you give an exemple of file before / after , please.
.
EDIT 2
Here's the code to change the values of certain variables in a file:
import re
from os import fsync
def updating(filename,dico):
RE = '(('+'|'.join(dico.keys())+')\s*=)[^\r\n]*?(\r?\n|\r)'
pat = re.compile(RE)
def jojo(mat,dic = dico ):
return dic[mat.group(2)].join(mat.group(1,3))
with open(filename,'rb') as f:
content = f.read()
with open(filename,'wb') as f:
f.write(pat.sub(jojo,content))
#-----------------------------------------------------------
vars = ['ENABLEPRINTER','PRINTERLIST','PRNT1.PORT']
new_values = ['y','PRNT3','8310']
what_to_change = dict(zip(vars,new_values))
updating('configfile_1.txt',what_to_change)
Before:
ENABLEPRINTER=n
PRINTERLIST=PRNT1, PRNT2
PRNT1.PORT=9600
PRNT1.BITS=8
After:
ENABLEPRINTER=y
PRINTERLIST=PRNT3
PRNT1.PORT=8310
PRNT1.BITS=8
这篇关于通过python编辑配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!