python基礎13 ---函數模組4(configparser模組)

來源:互聯網
上載者:User

標籤:live   添加   ring   user   options   操作   string   多個參數   write   

configparser模組

一、configparser模組

  1、什麼是configparser模組:configparser模組操作設定檔,設定檔的格式與windows ini和linux的cf檔案類似,可以包含一個或多個節(section),每個節可以有多個參數(鍵=值),其設定檔(INI檔案)由節(section)、鍵、值組成。

  2、configparser模組簡介。

  ConfigParser 是用來讀取設定檔的包。設定檔的格式如下:中括弧“[ ]”內包含的為section。section 下面為類似於key-value 的配置內容。

  3、如何產生一個configparser設定檔。

    1.首先import ConfigParser來調用ConfigParser模組 #import configparser

    2.建立一個ConfigParser設定檔對象。#config = configparser.ConfigParser()

    3.往設定檔中新增內容。#config["DEFAULT"] = {‘ServerAliveInterval‘: ‘45‘, ‘Compression‘: ‘yes‘ }  ; config[‘bitbucket.org‘] = {‘User‘:‘hg‘}

    4.開啟或建立一個設定檔,然後將設定檔對象內的東西添加到設定檔中。

      with open(‘example.ini‘, ‘w‘) as configfile:

        config.write(configfile)

    5.讀取該設定檔。#config.read(‘設定檔名稱‘)

    6.使用configparser模組解析設定檔時,發現的問題:

 

      參數名稱的大寫全部會轉換為小寫。

      參數名稱不能含有[,]

      如果含有多個名字相同的section時,會以最後一個section為準

    7.讀取設定檔和寫入設定檔。

    -read(filename) 直接讀取ini檔案內容
    -sections() 得到所有的section,並以列表的形式返回
    -options(section) 得到該section的所有option
    -items(section) 得到該section的所有索引值對
    -get(section,option) 得到section中option的值,返回為string類型
    -getint(section,option) 得到section中option的值,返回為int類型

    -add_section(section) 添加一個新的section
    -set( section, option, value) 對section中的option進行設定

    8.案例測試

      詳見:http://blog.csdn.net/gexiaobaohelloworld/article/details/7976944

二、subprocess模組

  1、

   

python基礎13 ---函數模組4(configparser模組)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.