+ -
当前位置:首页 → 问答吧 → python修改配置项的问题

python修改配置项的问题

时间:2011-09-16

来源:互联网

conf.1 中配置项有:
a:1
b:2
c:3

conf.2中配置项有
b:4
d:5

要实现的功能是:如果conf.2中的配置项中,在conf.1中存在的,则用2中的配置项修改1,如果conf.2有,
但conf.1没有的配置项,则将该配置项写入到conf.1中,如果用python该怎么实现这个功能?

作者: zhdeying2009   发布时间: 2011-09-16

以字典的形式读出来 ,然后update就Ok了

Python code

dict1 = {"a":1,"b":2,"c":3}
dict2 = {"d":4,"e":5}

for key,value in dict2.items():
    dict1.update({key:value})

print "dict1",dict1

作者: thundor   发布时间: 2011-09-16

把最直观的思路给你写一下吧
1. 把conf.1读进字典,生成{"a": 1, "b": 2, "c": 3}。假设保存为conf1。
2. 把conf.2也读进一个字典,保存成conf2。
3. 调用conf1.update(conf2)更新conf1。
4. 遍历conf1,把值写回文件conf.1。

没什么复杂的,自己试着写一下吧。

作者: iambic   发布时间: 2011-09-16