zoukankan      html  css  js  c++  java
  • 🍖configparser 模块 (了解)

    本文来自 :https://www.cnblogs.com/yang1333/articles/12609714.html#3177870913

    一. 配置文件如下

    [section1]
    k1 = v1
    k2:v2  # configparser配置文件也可以使用":"冒号, 关联option及对应的值
    user=egon
    age=18
    is_admin=true
    salary=31
    
    [section2]
    k1 = v1
    

    二. 使用configparser模块读取文件

    import configparser
    
    """
    # 因为python2的模块名命名的规范没有统一, 所以在python2中导入方式是:  import ConfigParser
    # 基于这种模块的运用, 文件的后缀名结尾一般命名成2种格式: 文件.ini 或者 文件.cfg
    """
    config = configparser.ConfigParser()
    config.read('a.cfg')
    
    # 查看所有的标题: 获取文件中所有的"sections". 返回值一个列表类型, 列表中的元素。对应着每一个section。
    print(config.sections())  # ['section1', 'section2']
    
    # 查看标题section1下所有key=value的key: 获取文件中对应的"section1"下所有的"options"。返回值一个列表类型, 列表中的元素。对应着当前"section"中的每一个option
    print(config.options('section1'))  # ['k1', 'k2', 'user', 'age', 'is_admin', 'salary']
    
    # 查看标题section1下所有key=value的(key,value)格式: 返回类似于这种格式[(option1, value1), (option2, value2)]
    print(config.items('section1'))  # [('k1', 'v1'), ('k2', 'v2'), ('user', 'egon'), ('age', '18'), ('is_admin', 'true'), ('salary', '31.1')]
    
    # 查看标题section1下user的值: 获取文件中"section1"下的"user"这个options对应的值. 返回的是字符串类型
    print(config.get('section1', 'k2'))  # v2
    res = config.get('section1', 'user')
    print(res, type(res))  # egon <class 'str'>
    
    # 查看标题section1下age的值: 返回整型int类型
    res = int(config.get('section1', 'age'))
    print(res, type(res))  # 18 <class 'int'>
    res = config.getint('section1', 'age') # 替代上面使用int的方式
    print(res, type(res))  # 18 <class 'int'>
    
    # 查看标题section1下is_admin的值: 返回布尔值
    res = config.getboolean('section1', 'is_admin')
    print(res, type(res))  # True <class 'bool'>
    
    # 查看标题section1下salary的值: 返回浮点型
    res = config.getfloat('section1', 'salary')
    print(res, type(res))  # 31.1 <class 'float'>
    """
    

    三. 使用configparser模块修改文件

    import configparser
    
    config = configparser.ConfigParser()
    config.read('a.cfg', encoding='utf-8')
    
    # 删除整个标题section2
    config.remove_section('section2')
    
    # 删除标题section1下的某个k1和k2
    config.remove_option('section1', 'k1')
    config.remove_option('section1', 'k2')
    
    # 判断是否存在某个标题
    print(config.has_section('section1'))
    
    # 判断标题section1下是否有user
    print(config.has_option('section1', ''))
    
    # 添加一个标题
    config.add_section('egon')
    
    # 在标题egon下添加name=egon,age=18的配置
    config.set('egon', 'name', 'egon')
    config.set('egon', 'age', 18)  # 报错,必须是字符串
    
    # 最后将修改的内容写入文件,完成最终的修改
    config.write(open('a.cfg', 'w'))
    

    四. 使用configparser模块添加一个ini格式的文件

    import configparser
    
    config = configparser.ConfigParser()
    config["DEFAULT"] = {'ServerAliveInterval': '45',
                         'Compression': 'yes',
                         'CompressionLevel': '9'}
    
    config['bitbucket.org'] = {}
    config['bitbucket.org']['User'] = 'hg'
    config['topsecret.server.com'] = {}
    topsecret = config['topsecret.server.com']
    topsecret['Host Port'] = '50022'  # mutates the parser
    topsecret['ForwardX11'] = 'no'  # same here
    config['DEFAULT']['ForwardX11'] = 'yes'
    with open('example.ini', 'w') as configfile:
        config.write(configfile)
    
  • 相关阅读:
    2019课设---基于微信小程序的食堂订餐送餐系统设计 【构思】(12)
    宝塔安装
    win10添加新建文本文档的快捷方式
    Vue底部菜单模块
    CSS渐变
    记录页面位置及进入页面时跳转位置
    PHP获取微信JS-SDK接口设置(access_token、jsapi_ticket、signature)
    添加Notepad++至右键菜单
    win10卸载XShell6报错1603
    win10我的电脑左侧快捷方式的控制
  • 原文地址:https://www.cnblogs.com/songhaixing/p/14275079.html
Copyright © 2011-2022 走看看