学会使用Python Configparser处理ini文件模块

 更新时间:2023年06月13日 11:22:56   作者:狄仁杰666  
这篇文章主要为大家介绍了使用Python Configparser处理ini文件模块的学习,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

Configparser 模块

本文知识点主要来源于网络上的文章,比较分散就没有放文章链接了~

学习路径

Configparser 模块简介;

  • Configparser 模块安装;

使用 Configparser 模块;

1. Configparser 模块简介;

在 python 日常开发工作当中,我们可以用 py 文件、ini、xml、excel、yaml、json、toml 等来管理我们的配置信息,伴随而来的是对这些文件处理的方法,Configparser 模块就是用来处理 ini 文件的模块。

2. Configparser 模块安装;

pip3 install configparser

安装超级快,说明模块很小~

方法

除了一些系统定义的函数和一些常量,也就这么些方法,不多。接下来我们一个一个用用看~

3. 使用 Configparser 模块;

代码演示:

import configparser
import os
def test():
    # 初始化实例并读取配置文件:
    config_parser = configparser.ConfigParser()
    config_parser.read("config.ini", encoding="utf-8")
    print("获取所用 section 节点:", config_parser.sections())
    print("获取指定 section 的 options,即将配置文件中的某个 section 内的所有 key 读取到列表中:", config_parser.options("section1"))
    print("判断配置文件中是否有某个 section:", config_parser.has_section("demo"))
    print("判断某个 section 下是否有某个 option:", config_parser.has_option("section1", "age"))
    print("判断某个 section 下是否有某个 option:", config_parser.has_option("section1", "developer"))
    print("获取指定 section 的指定 option(值是字符串或其他数字类型都可以):", config_parser.get("section1", "name"))
    print("获取指定 section 的指定 option(值是字符串或其他数字类型都可以):", config_parser.get("section1", "age"))
    print("获取指定 section 的指定 option(值是字符串或其他数字类型都可以):", config_parser.get("section2", "name"))
    print("获取指定 section 的指定 option(值必须是整数型数据,否则报错):", config_parser.getint("section1", "age"))
    print("获取指定 section 的指定 option(值必须是浮点型数据,否则报错):", config_parser.getfloat("section1", "weight"))
    print("获取指定 section 的指定 option(值必须是 boolean 值型数据,否则报错):", config_parser.getboolean("section1", "student"))
    # 添加 section 和 option
    config_parser.add_section("section3")
    config_parser.set("section3", "name", "baby")
    config_parser.write(open("config.ini", "w"))
    # 删除 section 和 option
    config_parser.remove_option("section3", "name")
    config_parser.remove_section("section3")
    config_parser.write(open("config.ini", "w"))
    # 高级用法 1:从字典中读取配置
    config_dict = {
        "oracle": {
            "hostname": "127.0.0.1",
            "username": "admin",
            "password": "admin123"
        }
    }
    config_parser.read_dict(config_dict)
    print("从字典中读取配置:", config_parser.get("oracle", "hostname"))
    # 高级用法 2:从字符串中读取配置
    config_str = """
        [section4]
        version = 1.0.1
        author = dylan.zhang
        date = 2023.3.14
    """
    config_parser.read_string(config_str)
    print("从字符串中读取配置:", config_parser.get("section4", "version"))
    # 高级用法 3:从文件对象中读取配置
    with open('config.ini') as config_file:
        config_parser.read_file(config_file)
        print("从文件对象中读取配置:", config_parser.get("section1", "height"))
        config_file.close()
    # 高级用法 4:读取 section 并返回可迭代的列表
    config_items = config_parser.items("section1")
    for key, value in config_items:
        print("迭代读取 section1 配置:", key, value)
    for index, value in enumerate(config_items):
        print("迭代读取 section1 配置:", index, value)
if __name__ == '__main__':
    test()

执行代码演示:

至此,我们对 Configparser 模块的使用已经基本探索完毕,其接口简单,相对全面,是读取 ini 配置文件的好帮手~

以上就是学会使用Python Configparser处理ini文件模块的详细内容,更多关于Python Configparser处理ini的资料请关注脚本之家其它相关文章!

相关文章

  • pytorch 多个反向传播操作

    pytorch 多个反向传播操作

    这篇文章主要介绍了pytorch 多个反向传播操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-05-05
  • 68行Python代码实现带难度升级的贪吃蛇

    68行Python代码实现带难度升级的贪吃蛇

    本文主要介绍了Python代码实现带难度升级的贪吃蛇,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • Python入门之三角函数全解【收藏】

    Python入门之三角函数全解【收藏】

    这篇文章主要介绍了Python入门之三角函数全解【收藏】,还是比较全面的,具有一定参考价值,需要的朋友可以了解下。
    2017-11-11
  • pytest-fixture简介及其用法讲解

    pytest-fixture简介及其用法讲解

    这篇文章主要介绍了pytest-fixture及其用法,最基本的用法就是一个fixture作为一个测试用例的参数传入,然后就可以在该测试用例中使用该fixture,需要的朋友可以参考下
    2023-01-01
  • Python双向循环链表实现方法分析

    Python双向循环链表实现方法分析

    这篇文章主要介绍了Python双向循环链表,结合实例形式分析了Python双向链表的定义、遍历、添加、删除、搜索等相关操作技巧,需要的朋友可以参考下
    2018-07-07
  • python的XIsxWriter操作Excel示例详解

    python的XIsxWriter操作Excel示例详解

    这篇文章主要介绍了python的XIsxWriter操作Excel示例详解,xlsxwriter是一个专门用于创建、写入和操作Excel文件的Python模块,它提供了丰富的功能和选项,能够创建复杂的Excel文档,需要的朋友可以参考下
    2023-09-09
  • Python中使用dwebsocket实现后端数据实时刷新

    Python中使用dwebsocket实现后端数据实时刷新

    dwebsocket是Python中一款用于实现WebSocket协议的库,可用于后端数据实时刷新。在Django中结合使用dwebsocket和Channels,可以实现前后端的实时通信,支持双向数据传输和消息推送,适用于实时聊天、数据监控、在线游戏等场景
    2023-04-04
  • Python是编译运行的验证方法

    Python是编译运行的验证方法

    这篇文章主要介绍了Python是编译运行的验证方法,本文讲解了一个小方法来验证Python是编译运行还是解释运行,需要的朋友可以参考下
    2015-01-01
  • Python读写二进制文件的实现

    Python读写二进制文件的实现

    本文主要介绍了Python读写二进制文件的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-04-04
  • Python多线程编程(四):使用Lock互斥锁

    Python多线程编程(四):使用Lock互斥锁

    这篇文章主要介绍了Python多线程编程(四):使用Lock互斥锁,本文讲解了互斥锁概念、同步阻塞、代码示例等内容,需要的朋友可以参考下
    2015-04-04

最新评论