乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      python 操作ini 配置文件大全

       刮骨劍 2019-07-21

      原文鏈接:https://www.cnblogs.com/bert227/p/9326313.html

       

      ConfigParser模塊在python中是用來(lái)讀取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一個(gè)或多個(gè)節(jié)(section),每個(gè)節(jié)可以有多個(gè)參數(shù)(鍵=值)。 
      注意:在python 3 中ConfigParser模塊名已更名為configparser

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      config.read('example.ini',encoding="utf-8")
      """讀取配置文件,python3可以不加encoding"""
      options(section)
      """sections(): 得到所有的section,并以列表的形式返回"""
      config.defaults()
      """defaults():返回一個(gè)包含實(shí)例范圍默認(rèn)值的詞典"""
      config.add_section(section)
      """添加一個(gè)新的section"""
      config.has_section(section)
      """判斷是否有section"""
      print(config.options(section))
      """得到該section的所有option"""
      has_option(section, option)
      """判斷如果section和option都存在則返回True否則False"""
      read_file(f, source=None)
      """讀取配置文件內(nèi)容,f必須是unicode"""
      read_string(string, source=’’)
      """從字符串解析配置數(shù)據(jù)"""
      read_dict(dictionary, source=’’)
      """從詞典解析配置數(shù)據(jù)"""
      get(section, option, *, raw=False, vars=None[, fallback])
      """得到section中option的值,返回為string類(lèi)型"""
      getint(section,option)
      """得到section中option的值,返回為int類(lèi)型"""
      getfloat(section,option)
      """得到section中option的值,返回為float類(lèi)型"""
      getboolean(section, option)
      """得到section中option的值,返回為boolean類(lèi)型"""
      items(raw=False, vars=None)
      """和items(section, raw=False, vars=None):列出選項(xiàng)的名稱(chēng)和值"""
      set(section, option, value)
      """對(duì)section中的option進(jìn)行設(shè)置"""
      write(fileobject, space_around_delimiters=True)
      """將內(nèi)容寫(xiě)入配置文件。"""
      remove_option(section, option)
      """從指定section移除option"""
      remove_section(section)
      """移除section"""
      optionxform(option)
      """將輸入文件中,或客戶(hù)端代碼傳遞的option名轉(zhuǎn)化成內(nèi)部結(jié)構(gòu)使用的形式。默認(rèn)實(shí)現(xiàn)返回option的小寫(xiě)形式;"""
      readfp(fp, filename=None)
      """從文件fp中解析數(shù)據(jù)"""

      生成configparser文件實(shí)例

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      import configparser  #配置文件
      config = configparser.ConfigParser()
      """生成configparser配置文件 ,字典的形式"""
      """第一種寫(xiě)法"""
      config["DEFAULT"] = {'ServerAliveInterval': '45',
                           'Compression': 'yes',
                           'CompressionLevel': '9'}
      """第二種寫(xiě)法"""
      config['bitbucket.org'] = {}
      config['bitbucket.org']['User'] = 'hg'
      """第三種寫(xiě)法"""
      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'
      """寫(xiě)入后綴為.ini的文件"""
      with open('example.ini', 'w') as configfile:
          config.write(configfile)

      運(yùn)行結(jié)果:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      [DEFAULT]
      serveraliveinterval = 45
      compression = yes
      compressionlevel = 9
      forwardx11 = yes
      [bitbucket.org]
      user = hg
      [topsecret.server.com]
      host port = 50022
      forwardx11 = no

      讀取configparser配置文件的實(shí)例

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      import configparser  #配置文件
      config = configparser.ConfigParser()
      config.read("example.ini")
      print("所有節(jié)點(diǎn)==>",config.sections())
      print("包含實(shí)例范圍默認(rèn)值的詞典==>",config.defaults())
      for item in config["DEFAULT"]:
          print("循環(huán)節(jié)點(diǎn)topsecret.server.com下所有option==>",item)
      print("bitbucket.org節(jié)點(diǎn)下所有option的key,包括默認(rèn)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é)點(diǎn)是否存在==>",'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"))

      運(yùn)行結(jié)果

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

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

      1
      2
      3
      4
      5
      6
      7
      8
      import configparser  #配置文件
      config = configparser.ConfigParser()
      config.read("example.ini")
      config.remove_section("bitbucket.org")
      """刪除分組"""
      config.remove_option("topsecret.server.com","host port")
      """刪除某組下面的某個(gè)值"""
      config.write(open('example.ini', "w"))

      運(yùn)行結(jié)果

      1
      2
      3
      4
      5
      6
      7
      8
      [DEFAULT]
      serveraliveinterval = 45
      compression = yes
      compressionlevel = 9
      forwardx11 = yes
      [topsecret.server.com]
      forwardx11 = no

      配置文件的修改實(shí)例

      1
      2
      3
      4
      5
      6
      7
      8
      9
      """修改"""
      import configparser
      config = configparser.ConfigParser()
      config.read("example.ini")
      config.add_section("new_section")
      """新增分組"""
      config.set("DEFAULT","compressionlevel","110")
      """設(shè)置DEFAULT分組下compressionlevel的值為110"""
      config.write(open('example.ini', "w"))

      運(yùn)行結(jié)果

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      [DEFAULT]
      serveraliveinterval = 45
      compression = yes
      compressionlevel = 110
      forwardx11 = yes
      [topsecret.server.com]
      forwardx11 = no
      [new_section]

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶(hù) 評(píng)論公約