• 七、python ConfigParser模块



    1、Python2 ConfigParser

    添加链接描述

    1.1 Pyhton程序中读取配置文件

    # 方法一
    cp = configparser.ConfigParser()
    cp.read("conf.ini")
    
    print(cp.sections())  
    print(cp.options("db"))
    print(cp.get("db","db_user")) 
    
    # 方法二
    import configparser
     
    cp = configparser.ConfigParser(allow_no_value=True)
    cp.read("conf.ini")
    data = cp.items("db")
    print(data)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    1.2 ConfigParser模块的常用方法

    read(filename) 直接读取ini文件内容
    sections() 得到所有的section,并以列表的形式返回
    options(section) 得到该section的所有option
    items(section) 得到该section的所有键值对
    get(section,option) 得到section中option的值,返回为string类型
    getint(section,option) 得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat() 函数
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2、Python3 configparser

    添加链接描述

    ConfigParser模块在Python3修改为configparser,这个模块定义了一个ConfigeParser类,该类的作用是让配置文件生效。配置文件的格式和window的ini文件相同

    2.1 ConfigParser()下的方法

    涉及增删改的操作 都需要使用write()方法之后才会生效

    add_section():用来新增section
    set():用来新增对应section下的某个键值对
    get()方法可以获得指定section下某个键的值
    options()返回对应section下可用的键
    remove_section()方法删除某个section
    remove_option()方法删除某个section下的键
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 代码示例
    
    import  configparser
     
    config = configparser.ConfigParser()
    file = 'D:/PycharmProjects/Vuiki/Config/config.ini'
    config.read(file)
    config.add_section('login')
    config.set('login','username','1111')
    config.set('login','password','2222')
    with open(file,'w') as configfile:
        config.write(configfile)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    3、写入配置文件

    python config文件的读写操作示例

    1、设置配置文件

    [mysql]
    host = 1234
    port = 3306
    user = root
    password = Zhsy08241128
    database = leartd
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2、读取配置文件

    import configparser
    import os
    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值
      print(name)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3、写入配置文件

    def writeConf():
      '''写入配置文件'''
      root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
      conf.read(root_path + '/ceshi/conf/app.conf') # 文件路径
      conf.set("mysql", "host", "1234") # 修改指定section 的option
      conf.write(open(root_path + '/ceshi/conf/app.conf', 'w'))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    three.js学习笔记
    翻页-时钟
    Day41—— 343. 整数拆分 96.不同的二叉搜索树 (动规)
    CMake中configure_file的使用
    计算机组成原理
    Redis企业开发的两种使用方式
    B_QuRT_User_Guide(34)
    springboot配置es集群两种方式
    手把手教你Nginx常用模块详解之ngx_http_rewrite_module(十)
    1164 Good in C – PAT甲级真题
  • 原文地址:https://blog.csdn.net/rui_rui96/article/details/126570116