zoukankan      html  css  js  c++  java
  • python小白-day6 ConfigParser模块

    ConfigParser模块

    用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser。

    来看一个好多软件的常见文档格式如下


    用python生成上文档代码如下;

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    import configparser
     
    config = 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'
    with open('example.ini', 'w') as configfile:
       config.write(configfile)

    读文档:

    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
    >>> import configparser
    >>> config = configparser.ConfigParser()
    >>> config.sections()
    []
    >>> config.read('example.ini')
    ['example.ini']
    >>> config.sections()
    ['bitbucket.org', 'topsecret.server.com']
    >>> 'bitbucket.org' in config
    True
    >>> 'bytebong.com' in config
    False
    >>> config['bitbucket.org']['User']
    'hg'
    >>> config['DEFAULT']['Compression']
    'yes'
    >>> topsecret = config['topsecret.server.com']
    >>> topsecret['ForwardX11']
    'no'
    >>> topsecret['Port']
    '50022'
    >>> for key in config['bitbucket.org']: print(key)
    ...
    user
    compressionlevel
    serveraliveinterval
    compression
    forwardx11
    >>> config['bitbucket.org']['ForwardX11']
    'yes'

    删除bitbucket.org

    1
    2
    3
    4
    5
    6
    import configparser
     
    config = configparser.ConfigParser()
    config.read('example.ini')
    config.remove_section('bitbucket.org')
    config.write(open('example.bak','w'))


    增加hetan:

    1
    2
    3
    4
    5
    6
    7
    8
    import configparser
     
    config = configparser.ConfigParser()
    config.read('example.ini')
    config.has_section('hetan')
    config.add_section('hetan')
    config['hetan']['age'] = "26"
    config.write(open('example.cfg','w'))


    修改hetan中的age为25:

    1
    2
    3
    4
    5
    6
    import configparser
     
    config = configparser.ConfigParser()
    config.read('example.cfg')
    config.set('hetan','age','25')
    config.write(open('example.cfg.bak','w'))


    删除hetan中的age:

    1
    2
    3
    4
    5
    6
    import configparser
     
    config = configparser.ConfigParser()
    config.read('example.cfg.bak')
    config.remove_option('hetan','age')
    config.write(open('example.cfg.bak2','w'))






  • 相关阅读:
    GridView小知识1
    ASP 中 GridView 的粗浅入门
    SQL连接
    Microsoft Visual Studio 2010 Express for Windows Phone 新建文件 设置启动
    转载一个应届计算机毕业生2012求职之路
    百度之星平衡负载(3.23)
    查找字符串中首个非重复字符
    CreateMutex函数
    关于“Visual Studio 遇到了异常,可能是由于某个扩展导致的”的解决
    无法打开预编译头文件:“Debug\****.pch”: No such file or directory 的解决办法
  • 原文地址:https://www.cnblogs.com/hetan/p/5211133.html
Copyright © 2011-2022 走看看