conf= configparser.ConfigParser() def readConf(): '''读取配置文件''' root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) conf.read(root_path + '/ceshi/conf/app.conf') # 文件路径 print(conf) name = conf.get("mysql", "host") # 获取指定section 的option值 pr...
In [2]: cnf = ConfigParser.ConfigParser() In [4]: cnf.read('set.ini') Out[4]: ['set.ini'] get(section, option)获取section下具体某一配置项的值(返回的是字符串) In [6]: cnf.get('mysql','db_port') Out[6]:'3306' sections()得到所有的section,并以列表的形式返回 In [7]: cnf....
cp.add_section('new_sect') j) 删除 section cp.remove_section('db') k) 保存配置,set、 remove_option、 add_section 和 remove_section 等操作并不会修改配置文件,write 方法可以将 ConfigParser 对象的配置写到文件中 cp.write(open('myapp.conf', 'w')) cp.write(sys.stdout) Unicode 编码的配置 ...
conf=configparser.ConfigParser()conf.read('sec',encoding='utf-8')# 检查节点has=conf.has_section('SEC_1')print(has)#添加节点conf.add_section('sec_3')conf.write(open('sec','w'))#删除节点conf.remove_section('SEC_2')conf.write(open('sec','w')) 1. 2. 3. 4. 5. 6. 7. 8. 9...
import configparser conf = configparser.ConfigParser() conf.read('config.ini') # 添加新配置项 def add_config(): ''' 要新增的内容 [api] name = /user/login method = 'get' body = {'username':'admin','password':'123456'} ''' conf.add_section('api') conf.set('api','nam...
exception ConfigParser.NoSectionError 找不到指定的部分时引发异常。 exception ConfigParser.DuplicateSectionError 如果add_section()使用已经存在的节的名称调用,则引发异常。 exception ConfigParser.NoOptionError 当在指定的部分找不到指定的选项时引发异常。
config = configparser.ConfigParser() config.read('C:/Users/../Desktop/streamer.conf') 然后它与此错误消息分开: MissingSectionHeaderError: File contains no section headers. file: 'C:/Users/../Desktop/streamer.conf', line: 1 u'input{\n' ...
configparser()模块 .section():获取节点形成列表 add_section():添加节点 remove_section:删除节点 excel文件操作 from openpyxl import load_workbook a=load_workbook(filename) 相关操作: sheetname:获取所有sheet名称 选择sheet:A[‘sheet名称’] 选择sheet内单元格:sheet.cell(x,y) 选择sheet基于索引位置:...
上面的demo.ini是一个非常基础的配置文件,它由多个部分(section)组成,每部分包含了带值的选项。ConfigParse类的实例可以对其进行读写操作。 创建配置文件: 代码语言:javascript 代码运行次数:0 运行 AI代码解释 importconfigparser config=configparser.ConfigParser()config["DEFAULT"]={"ServerAliveInterval":"45","Compr...