Python 简洁ini读写
[代码] [Python]代码
05 |
def writeConfig(filename): |
06 |
config = ConfigParser.ConfigParser() |
09 |
config.add_section( section_name ) |
10 |
config. set ( section_name, 'dbname' , 'MySQL' ) |
11 |
config. set ( section_name, 'host' , '127.0.0.1' ) |
12 |
config. set ( section_name, 'port' , '80' ) |
13 |
config. set ( section_name, 'password' , '123456' ) |
14 |
config. set ( section_name, 'databasename' , 'test' ) |
18 |
config.add_section( section_name ) |
19 |
config. set ( section_name, 'loggerapp' , '192.168.20.2' ) |
20 |
config. set ( section_name, 'reportapp' , '192.168.20.3' ) |
23 |
config.write( open (filename, 'a' ) ) |
25 |
def updateConfig(filename, section, * * keyv): |
26 |
config = ConfigParser.ConfigParser() |
28 |
[config. set (section, key, keyv[key]) for key in keyv if config.has_option(section, key)] |
29 |
config.write( open (filename, 'r+' ) ) |
31 |
if __name__ = = '__main__' : |
32 |
file_name = 'test.ini' |
33 |
writeConfig(file_name) |
34 |
updateConfig(file_name, 'app' , reportapp = '192.168.100.100' ) |
41 |
def writeConfig(filename): |
42 |
config = ConfigParser.ConfigParser() |
45 |
config.add_section( section_name ) |
46 |
config. set ( section_name, 'dbname' , 'MySQL' ) |
47 |
config. set ( section_name, 'host' , '127.0.0.1' ) |
48 |
config. set ( section_name, 'port' , '80' ) |
49 |
config. set ( section_name, 'password' , '123456' ) |
50 |
config. set ( section_name, 'databasename' , 'test' ) |
54 |
config.add_section( section_name ) |
55 |
config. set ( section_name, 'loggerapp' , '192.168.20.2' ) |
56 |
config. set ( section_name, 'reportapp' , '192.168.20.3' ) |
59 |
config.write( open (filename, 'a' ) ) |
61 |
def updateConfig(filename, section, * * keyv): |
62 |
config = ConfigParser.ConfigParser() |
64 |
[config. set (section, key, keyv[key]) for key in keyv if config.has_option(section, key)] |
65 |
config.write( open (filename, 'r+' ) ) |
67 |
if __name__ = = '__main__' : |
68 |
file_name = 'test.ini' |
69 |
writeConfig(file_name) |
70 |
updateConfig(file_name, 'app' , reportapp = '192.168.100.100' ) |
|