zoukankan      html  css  js  c++  java
  • python读取ini配置文件-configparser使用方法

    我们在操作 ini 配置文件的时候 可以使用 Python 的 configparser 库

    具体使用方法如下:

    from configparser import ConfigParser
    
    # 初始化
    cf = ConfigParser()
    
    # 加载文件
    cf.read('ini.ini')
    
    
    # 读取 user 节点下所有数据
    all = cf.items('user')
    print(all)
    
    # 读取 user 节点下 name 的值
    name = cf.get('user', 'name')
    print(name)
    
    
    # 增加节点
    cf.add_section('teacher')
    cf.add_section('test')
    
    
    # 删除 test 节点
    cf.remove_section('test')
    
    
    
    # 给指定节点添加信息
    cf.set('user', 'sex', '')
    cf.set('teacher', '语文老师', '张老师')
    
    
    
    # 修改 user 节点下的 age
    cf.set('user', 'age', '90')
    
    
    # 删除 user 节点下的 sex
    cf.remove_option('user', 'sex')
    
    
    # 保存到文件
    cf.write(open('a.ini', 'w', encoding='utf-8'))

    原 ini 文件

    修改后保存的 a.ini 文件

  • 相关阅读:
    点击有惊喜
    模态框案例
    DOM操作
    定时器
    函数和object
    shell 判断文件出现次数
    shell 判断路径
    shell 循环数组
    shell 判断为空打印
    shell 示例1 从1叠加到100
  • 原文地址:https://www.cnblogs.com/shiyixirui/p/12931826.html
Copyright © 2011-2022 走看看