1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關咨詢
      選擇下列產品馬上在線溝通
      服務時間:8:30-17:00
      你可能遇到了下面的問題
      關閉右側工具欄

      新聞中心

      這里有您想知道的互聯(lián)網營銷解決方案
      Python的configparse模塊-創(chuàng)新互聯(lián)

      創(chuàng)新互聯(lián)www.cdcxhl.cn八線動態(tài)BGP香港云服務器提供商,新人活動買多久送多久,劃算不套路!

      成都創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網綜合服務,包含不限于網站制作、做網站、淮陰網絡推廣、小程序設計、淮陰網絡營銷、淮陰企業(yè)策劃、淮陰品牌公關、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務,您的肯定,是我們大的嘉獎;成都創(chuàng)新互聯(lián)為所有大學生創(chuàng)業(yè)者提供淮陰建站搭建服務,24小時服務熱線:13518219792,官方網址:www.cdcxhl.com

      本篇文章給大家分享的是有關Python的configparse模塊,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

      常用模塊 - configparse模塊

      一、簡介

      configparser模塊在Python中是用來讀取配置文件的,配置文件的格式跟windows下的ini配置文件相似,可以包含一個或多個節(jié)點(section),每個節(jié)可以有多個參數(shù)(鍵=值)。

      二、生成配置文件

      #! /usr/bin/env python3
      # -*- coding:utf-8 -*-
      # Note     : 用于測試configparser模塊的功能
      # 導入模塊
      import configparser
      config = configparser.ConfigParser()
      """生成configparser配置文件 ,字典的形式"""
      """第一種寫法"""
      config["DEFAULT"] = {'ServerAliveInterval': '45',
                           'Compression': 'yes',
                           'CompressionLevel': '9'}
      """第二種寫法"""
      config['bitbucket.org'] = {}
      config['bitbucket.org']['User'] = 'hg'
      """第三種寫法"""
      config['topsecret.server.com'] = {}
      topsecret = config['topsecret.server.com']
      topsecret['Host Port'] = '50022'  # mutates the parser
      topsecret['ForwardX11'] = 'no'  # same here
      config['DEFAULT']['ForwardX11'] = 'yes'
      """寫入后綴為.ini的文件"""
      with open('example.ini', 'w') as configfile:
          config.write(configfile)

      運行后,文件“example.ini”中的結果:

      [DEFAULT]
      compression = yes
      compressionlevel = 9
      serveraliveinterval = 45
      forwardx11 = yes
      [bitbucket.org]
      user = hg
      [topsecret.server.com]
      host port = 50022
      forwardx11 = no

      三、解析配置文件

      讀取configparser配置文件的實例

      #! /usr/bin/env python3
      # -*- coding:utf-8 -*-
      # Note     : 用于測試configparser模塊的功能
      # 導入模塊
      import configparser
      config = configparser.ConfigParser()
      # 讀取配置文件
      config.read("example.ini")
      print("所有節(jié)點==>", config.sections())
      print("包含實例范圍默認值的詞典==>", config.defaults())
      for item in config["DEFAULT"]:
          print("循環(huán)節(jié)點topsecret.server.com下所有option==>", item)
      print("bitbucket.org節(jié)點下所有option的key,包括默認option==>", config.options("bitbucket.org"))
      print("輸出元組,包括option的key和value", config.items('bitbucket.org'))
      print("bitbucket.org下user的值==>", config["bitbucket.org"]["user"]) # 方式一
      topsecret = config['bitbucket.org']
      print("bitbucket.org下user的值==>", topsecret["user"]) # 方式二
      print("判斷bitbucket.org節(jié)點是否存在==>", 'bitbucket.org' in config)
      print("獲取bitbucket.org下user的值==>", config.get("bitbucket.org","user"))
      print("獲取option值為數(shù)字的:host port=", config.getint("topsecret.server.com","host port"))

      運行結果:

      所有節(jié)點==> ['bitbucket.org', 'topsecret.server.com']
      包含實例范圍默認值的詞典==> OrderedDict([('compression', 'yes'), ('compressionlevel', '9'), ('serveraliveinterval', 
      '45'), ('forwardx11', 'yes')])
      循環(huán)節(jié)點topsecret.server.com下所有option==> compression
      循環(huán)節(jié)點topsecret.server.com下所有option==> compressionlevel
      循環(huán)節(jié)點topsecret.server.com下所有option==> serveraliveinterval
      循環(huán)節(jié)點topsecret.server.com下所有option==> forwardx11
      bitbucket.org節(jié)點下所有option的key,包括默認option==> ['user', 'compression', 'compressionlevel', 
      'serveraliveinterval', 'forwardx11']
      輸出元組,包括option的key和value [('compression', 'yes'), ('compressionlevel', '9'), ('serveraliveinterval', '45'), 
      ('forwardx11', 'yes'), ('user', 'hg')]
      bitbucket.org下user的值==> hg
      bitbucket.org下user的值==> hg
      判斷bitbucket.org節(jié)點是否存在==> True
      獲取bitbucket.org下user的值==> hg
      獲取option值為數(shù)字的:host port= 50022

      刪除配置文件section和option的實例(默認分組有參數(shù)時無法刪除,但可以先刪除下面的option,再刪分組)

      #! /usr/bin/env python3
      # -*- coding:utf-8 -*-
      # Note     : 用于測試configparser模塊的功能
      # 導入模塊
      import configparser
      config = configparser.ConfigParser()
      # 讀取配置文件
      config.read("example.ini")
      config.remove_section("bitbucket.org")
      """刪除分組"""
      config.remove_option("topsecret.server.com", "host port")
      """刪除某組下面的某個值"""
      config.write(open('example.ini', "w"))

      運行后,文件“example.ini”中的結果:

      [DEFAULT]
      compression = yes
      compressionlevel = 9
      serveraliveinterval = 45
      forwardx11 = yes
      [topsecret.server.com]
      forwardx11 = no

      修改配置文件

      #! /usr/bin/env python3
      # -*- coding:utf-8 -*-
      # Note     : 用于測試configparser模塊的功能
      # 導入模塊
      import configparser
      config = configparser.ConfigParser()
      # 讀取配置文件
      config.read("example.ini")
      config.add_section("new_section")
      """新增分組"""
      config.set("DEFAULT", "compressionlevel", "110")
      """設置DEFAULT分組下compressionlevel的值為110"""
      config.write(open('example.ini', "w"))

      運行后,文件“example.ini”中的結果:

      [DEFAULT]
      compression = yes
      compressionlevel = 110
      serveraliveinterval = 45
      forwardx11 = yes
      [topsecret.server.com]
      forwardx11 = no
      [new_section]

      以上就是Python的configparse模塊,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注創(chuàng)新互聯(lián)-成都網站建設公司行業(yè)資訊頻道。


      分享文章:Python的configparse模塊-創(chuàng)新互聯(lián)
      新聞來源:http://ef60e0e.cn/article/degjjp.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        连云港市| 丰顺县| 客服| 郧西县| 肃宁县| 浑源县| 依兰县| 玛沁县| 长葛市| 万源市| 济源市| 新宁县| 河间市| 霸州市| 天水市| 嘉善县| 库车县| 万载县| 那曲县| 建宁县| 华蓥市| 探索| 札达县| 武威市| 上蔡县| 景德镇市| 乳山市| 大余县| 淅川县| 新民市| 清苑县| 宣武区| 阜南县| 天峨县| 西青区| 浪卡子县| 清水县| 江山市| 北辰区| 梁山县| 望谟县|