config.read_file(fn,source=None)#解析一个文件对象(通俗的讲就是你打开一个文件之后,这就是一个文件对象) 代码语言:javascript 代码运行次数:0 运行 AI代码解释 config.read_string(string)#先将字符串转变问文件对象,然后执行上面那个方法 代码语言:javascript 代码运行次数:0 运行 AI代码解释 config.dict(dicti...
配置文件中可以引用其他选项,可以通过在ConfigParser构造函数中传入interpolation参数来进行控制。 代码语言:javascript 代码运行次数:0 运行 AI代码解释 c=configparser.ConfigParser(interpolation=configparser.ExtendedInterpolation())c.read_string(s)print(c['young']['age']) 默认的插入是configparser.BasicInterpolation(...
ConfigParser() # 初始化对象 file_path = "D:/ApiTest/Other/test.ini" # 定义文件路径 cfg.read(file_path) # 读取文件 host = cfg.get('db', 'host') # get(section,option) 得到section中option的值,返回为string类型 sections = cfg.sections() # sections()得到所有的section,并以列表的形式返回...
importconfigparser file ='config.ini'# 创建配置文件对象cfg = configparser.ConfigParser()# 读取配置文件cfg.read(file, encoding='utf-8') 这里只打开不做什么读取和改变 回到顶部 读取配置文件的所有 section file处替换为对应的配置文件即可 importconfigparser file ='config.ini'cfg = configparser.ConfigParser...
config.read_string(config_string) 1. 4. 访问配置信息 读取配置信息后,我们可以通过ConfigParser对象的方法来访问配置项的值。例如,可以使用get方法来获取指定section和option的值。 value=config.get(section,option) 1. 完整代码示例: importConfigParser# 配置文件字符串config_string=""" ...
6. read_file(f, filename=None) 读取配置文件的内容到对象;这里的f是一个文件的句柄;例如: import configparser config = configparser.ConfigParser() f=open('example.ini') config.read_file(f) # 这两句等效 config.read('example.ini') 回到顶部 7. read_string(string) 从指定的字符串中读取配置到...
import configparser config = configparser.ConfigParser() f=open('example.ini') config.read_file(f) # 这两句等效 config.read('example.ini') 1. 2. 3. 4. 5. 6. 7. 8. 7. read_string(string) 从指定的字符串中读取配置到对象; 1. ...
python模块之configparser 快速开始 # demo.ini [DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 ForwardX11 = yes [bitbucket.org] User = hg [topsecret.server.com] Port = 50022 ForwardX11 = no 上面的demo.ini是一个非常基础的配置文件,它由多个部分(section)组成,每部分包含...
>>> parser = configparser.ConfigParser() >>> parser.read('exam.ini') ['exam.ini'] 可以同时读取多个文件,以文件列表作参数 >>> parser.read(['exam.ini', 'exam1.ini']) ['exam.ini', 'exam1.ini'] >>> parser.read(['exam.ini', 'exam2.ini']) ...
fromconfigparserimportConfigParser 实例化 config=ConfigParser() 3、读取config文件数据 常用方法: config.read(filename,encoding)直接读取ini文件内容,finlename 文件地址,encoding 文件编码格式 config.sections()得到所有的section,并以列表的形式返回 config.options(section)得到该section的所有option ...