zoukankan      html  css  js  c++  java
  • Python 之ConfigParser

    configparser用于处理特定格式的文件,其本质上是利用open来操作文件。
    
    
    复制代码
    # 注释1
    ;  注释2
     
    [section1] # 节点
    k1 = v1    #
    k2:v2       #
     
    [section2] # 节点
    k1 = v1    #
    复制代码
    1、获取所有节点
    
    import configparser
     
    config = configparser.ConfigParser()
    config.read('xxxooo', encoding='utf-8')
    ret = config.sections()
    print(ret)
    2、获取指定节点下所有的键值对
    
    import configparser
     
    config = configparser.ConfigParser()
    config.read('xxxooo', encoding='utf-8')
    ret = config.items('section1')
    print(ret)
    3、获取指定节点下所有的建
    
    import configparser
     
    config = configparser.ConfigParser()
    config.read('xxxooo', encoding='utf-8')
    ret = config.options('section1')
    print(ret)
    4、获取指定节点下指定key的值
    
    
    import configparser
     
    config = configparser.ConfigParser()
    config.read('xxxooo', encoding='utf-8')
     
     
    v = config.get('section1', 'k1')
    # v = config.getint('section1', 'k1')
    # v = config.getfloat('section1', 'k1')
    # v = config.getboolean('section1', 'k1')
     
    print(v)
    5、检查、删除、添加节点
    
    
    import configparser
     
    config = configparser.ConfigParser()
    config.read('xxxooo', encoding='utf-8')
     
     
    # 检查
    has_sec = config.has_section('section1')
    print(has_sec)
     
    # 添加节点
    config.add_section("SEC_1")
    config.write(open('xxxooo', 'w'))
     
    # 删除节点
    config.remove_section("SEC_1")
    config.write(open('xxxooo', 'w'))
    6、检查、删除、设置指定组内的键值对
    
    import configparser
     
    config = configparser.ConfigParser()
    config.read('xxxooo', encoding='utf-8')
     
    # 检查
    has_opt = config.has_option('section1', 'k1')
    print(has_opt)
     
    # 删除
    config.remove_option('section1', 'k1')
    config.write(open('xxxooo', 'w'))
     
    # 设置
    config.set('section1', 'k10', "123")
    config.write(open('xxxooo', 'w'))
  • 相关阅读:
    vue 组件传值
    ES6 解构赋值
    JS filter的使用
    FormData实现文件上传
    vue+element 表格导出Excel文件
    vue2.0 element-ui中input的@keyup.native.enter='onQuery'回车查询刷新整个表单的解决办法
    vue2.0 element-ui中el-upload的before-upload方法返回false时submit()不生效解决方法
    JavaScript正则表达式检验手机号码、邮箱、ip地址等
    Vue 2.0 pagination分页组件
    angular环境
  • 原文地址:https://www.cnblogs.com/Erick-L/p/6418057.html
Copyright © 2011-2022 走看看