欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

學會使用Python?Configparser處理ini文件模塊

 更新時間:2023年06月13日 11:22:56   作者:狄仁杰666  
這篇文章主要為大家介紹了使用Python?Configparser處理ini文件模塊的學習,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

Configparser 模塊

本文知識點主要來源于網絡上的文章,比較分散就沒有放文章鏈接了~

學習路徑

Configparser 模塊簡介;

  • Configparser 模塊安裝;

使用 Configparser 模塊;

1. Configparser 模塊簡介;

在 python 日常開發(fā)工作當中,我們可以用 py 文件、ini、xml、excel、yaml、json、toml 等來管理我們的配置信息,伴隨而來的是對這些文件處理的方法,Configparser 模塊就是用來處理 ini 文件的模塊。

2. Configparser 模塊安裝;

pip3 install configparser

安裝超級快,說明模塊很小~

方法

除了一些系統(tǒng)定義的函數(shù)和一些常量,也就這么些方法,不多。接下來我們一個一個用用看~

3. 使用 Configparser 模塊;

代碼演示:

import configparser
import os
def test():
    # 初始化實例并讀取配置文件:
    config_parser = configparser.ConfigParser()
    config_parser.read("config.ini", encoding="utf-8")
    print("獲取所用 section 節(jié)點:", 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(值是字符串或其他數(shù)字類型都可以):", config_parser.get("section1", "name"))
    print("獲取指定 section 的指定 option(值是字符串或其他數(shù)字類型都可以):", config_parser.get("section1", "age"))
    print("獲取指定 section 的指定 option(值是字符串或其他數(shù)字類型都可以):", config_parser.get("section2", "name"))
    print("獲取指定 section 的指定 option(值必須是整數(shù)型數(shù)據(jù),否則報錯):", config_parser.getint("section1", "age"))
    print("獲取指定 section 的指定 option(值必須是浮點型數(shù)據(jù),否則報錯):", config_parser.getfloat("section1", "weight"))
    print("獲取指定 section 的指定 option(值必須是 boolean 值型數(shù)據(jù),否則報錯):", 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()

執(zhí)行代碼演示:

至此,我們對 Configparser 模塊的使用已經基本探索完畢,其接口簡單,相對全面,是讀取 ini 配置文件的好幫手~

以上就是學會使用Python Configparser處理ini文件模塊的詳細內容,更多關于Python Configparser處理ini的資料請關注腳本之家其它相關文章!

相關文章

  • pytorch 多個反向傳播操作

    pytorch 多個反向傳播操作

    這篇文章主要介紹了pytorch 多個反向傳播操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-05-05
  • 68行Python代碼實現(xiàn)帶難度升級的貪吃蛇

    68行Python代碼實現(xiàn)帶難度升級的貪吃蛇

    本文主要介紹了Python代碼實現(xiàn)帶難度升級的貪吃蛇,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • Python入門之三角函數(shù)全解【收藏】

    Python入門之三角函數(shù)全解【收藏】

    這篇文章主要介紹了Python入門之三角函數(shù)全解【收藏】,還是比較全面的,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • pytest-fixture簡介及其用法講解

    pytest-fixture簡介及其用法講解

    這篇文章主要介紹了pytest-fixture及其用法,最基本的用法就是一個fixture作為一個測試用例的參數(shù)傳入,然后就可以在該測試用例中使用該fixture,需要的朋友可以參考下
    2023-01-01
  • Python雙向循環(huán)鏈表實現(xiàn)方法分析

    Python雙向循環(huán)鏈表實現(xiàn)方法分析

    這篇文章主要介紹了Python雙向循環(huán)鏈表,結合實例形式分析了Python雙向鏈表的定義、遍歷、添加、刪除、搜索等相關操作技巧,需要的朋友可以參考下
    2018-07-07
  • python的XIsxWriter操作Excel示例詳解

    python的XIsxWriter操作Excel示例詳解

    這篇文章主要介紹了python的XIsxWriter操作Excel示例詳解,xlsxwriter是一個專門用于創(chuàng)建、寫入和操作Excel文件的Python模塊,它提供了豐富的功能和選項,能夠創(chuàng)建復雜的Excel文檔,需要的朋友可以參考下
    2023-09-09
  • Python中使用dwebsocket實現(xiàn)后端數(shù)據(jù)實時刷新

    Python中使用dwebsocket實現(xiàn)后端數(shù)據(jù)實時刷新

    dwebsocket是Python中一款用于實現(xiàn)WebSocket協(xié)議的庫,可用于后端數(shù)據(jù)實時刷新。在Django中結合使用dwebsocket和Channels,可以實現(xiàn)前后端的實時通信,支持雙向數(shù)據(jù)傳輸和消息推送,適用于實時聊天、數(shù)據(jù)監(jiān)控、在線游戲等場景
    2023-04-04
  • Python是編譯運行的驗證方法

    Python是編譯運行的驗證方法

    這篇文章主要介紹了Python是編譯運行的驗證方法,本文講解了一個小方法來驗證Python是編譯運行還是解釋運行,需要的朋友可以參考下
    2015-01-01
  • Python讀寫二進制文件的實現(xiàn)

    Python讀寫二進制文件的實現(xiàn)

    本文主要介紹了Python讀寫二進制文件的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-04-04
  • Python多線程編程(四):使用Lock互斥鎖

    Python多線程編程(四):使用Lock互斥鎖

    這篇文章主要介紹了Python多線程編程(四):使用Lock互斥鎖,本文講解了互斥鎖概念、同步阻塞、代碼示例等內容,需要的朋友可以參考下
    2015-04-04

最新評論