config.sections() # 得到所有的section,并以列表的形式返回 config.options(section) # 得到该section的所有option (key值) config.items(section) # 得到该section的所有键值对,返回结果是列表中包含的tuple config.get(section, option) # 得到section中option的值,返回为string类型,指定标签下面的key对应的value值...
Configuration file parser. A setup file consists of sections, lead by a "[section]" header, and followed by "name: value" entries, with continuations and such in the style ofRFC 822. 该模块支持读取类似如上格式的配置文件,如 windows 下的 .conf 及 .ini 文件等。 本章节使用如下的配置文件作...
db_host = config['Database']['host'] db_port = config['Database'].getint('port') db_username = config['Database']['username'] db_password = config['Database']['password'] # 读取日志配置信息 log_level = config['Logging']['level'] log_file = config['Logging']['file'] 写入配...
Python 读取写入配置文件很方便,可使用内置的 configparser 模块;可查看源码,如博主本机地址: “C:/python27/lib/configparser.py” Configuration file parser. A setup file consists of sections, lead by a "[section]" header, and followed by "name: value" entries, with continuations and such in the ...
>>> topsecret['Port'] = '50022' # mutates the parser >>> topsecret['ForwardX11'] = 'no' # same here >>> config['DEFAULT']['ForwardX11'] = 'yes' >>> with open('example.ini', 'w') as configfile: ... config.write(configfile) ...
Configuration file parser. A configuration file consists of sections, lead by a “[section]” header, and followed by “name: value” entries, with continuations and such in the style of RFC 822. Note The ConfigParser module has been renamed to configparser in Python 3. The 2to3 tool will...
"""config_parser.read_string(config_str)print("从字符串中读取配置:",config_parser.get("section4","version"))# 高级用法 3:从文件对象中读取配置withopen('config.ini')asconfig_file:config_parser.read_file(config_file)print("从文件对象中读取配置:",config_parser.get("section1","height"))con...
config.readfp(open('defaults.cfg')) config.read(['site.cfg', os.path.expanduser('~/.myapp.cfg')]) cfgparser = ConfigParser() ... cfgparser.optionxform = str ConfigParser对象 SafeConfigParser中包含ConfigParser相同的方法,还有一部分增加的方法 ...
print(parser.sections())# ['settings', 'db', 'files']print(parser.get('settings','secret_key'))# abc123print(parser.options('settings'))# ['debug', 'secret_key', 'log_path']print('db'inparser)# True When loading from a config file, all data types will be returned as strings....
>>>topsecret['Port']='50022' # mutates the parser >>>topsecret['ForwardX11']='no' # same here >>>config['DEFAULT']['ForwardX11']='yes' >>>withopen('example.ini','w')asconfigfile: ... config.write(configfile) ... 如你所见,我们可以把配置解析器当作一个字典来处理。 两者确实存...