最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

Configparser 模塊

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

學習路徑

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 內(nèi)的所有 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 模塊的使用已經(jīng)基本探索完畢,其接口簡單,相對全面,是讀取 ini 配置文件的好幫手~

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

相關文章

最新評論

吴旗县| 宝山区| 疏附县| 道孚县| 吉林省| 武山县| 台山市| 页游| 永寿县| 漾濞| 定南县| 紫阳县| 嘉荫县| 仁布县| 合江县| 永兴县| 常宁市| 洱源县| 宜君县| 佛学| 太谷县| 南陵县| 资阳市| 阿拉善左旗| 隆德县| 新余市| 高邮市| 阜康市| 广水市| 綦江县| 桦川县| 安乡县| 平邑县| 绥德县| 扎鲁特旗| 久治县| 兴业县| 曲沃县| 道孚县| 南雄市| 英吉沙县|