#配置文件相关
import ConfigParser
def read_config(sec, opt, file):
conf = ConfigParser.ConfigParser()
conf.read(file)
key_value = conf.get(sec,opt)
print('read config input(%s,%s,%s) -> return value:%s' %(sec, opt, file,str(key_value)))
return key_value
def write_config(value, file):
num = 1
conf = ConfigParser.ConfigParser()
conf.read(file)
for info in value:
section = r'info%d'%num
conf.add_section(section )
conf.set(section , 'a', info[0])
conf.set(section , 'b', info[1])
conf.set(section , 'c', info[2])
conf.set(section , 'd', info[3])
num += 1
conf.write(open(file, "w"))
print('write config success')
if __name__ == '__main__':
info = [[1,2,3,4],[5,6,7,8]]
file = r'/home/py/test.ini'
write_config(info, file)
res = read_config('info1', 'a', file)
```
输出:
write config success
read config input(info1,a,/home/py/test.ini) -> return value:1
test.ini内容如下
[info1]
a = 1
b = 2
c = 3
d = 4
[info2]
a = 5
b = 6
c = 7
d = 8
python 操作配置文件
最新推荐文章于 2024-08-05 15:00:47 发布