zoukankan      html  css  js  c++  java
  • python模块--config

    一、创建文件

     1 ##-----------------创建数据表--------------------------
     2 import configparser
     3 config = configparser.ConfigParser()
     4 
     5 config["DEFAULT"] = {
     6     'ServerAliveInterval': '45',
     7     'Compression': 'yes',
     8     'CompressionLevel': '9'
     9 }
    10 
    11 config["bitbucket.org"] = {}
    12 config["bitbucket.org"]["user"] = "hg"
    13 
    14 config['topsecret.server.com'] = {}
    15 topsecret = config['topsecret.server.com'] #  ?
    16 topsecret['Host Post'] = '50022'  #mutates the parser
    17 topsecret['ForwardX11'] = 'no' #same here
    18 
    19 with open('example.ini','w') as f:
    20     config.write(f)

    2、增删改查--查

     1 ###----------增删改查------------------------------
     2 import configparser
     3 config = configparser.ConfigParser()
     4 
     5 
     6 print(config.sections())  # 返回空列表[]
     7 # print(config.user())
     8 ##------------> 查
     9 config.read('example.ini')
    10 print(config.sections())  #['bitbucket', 'topsecret.server.com']
    11 
    12 print('bytebong.com' in config)   #False
    13 print(config['bitbucket.org']['user'])   # hg
    14 
    15 print(config['topsecret.server.com']['host post'])   #50022
    16 print(config['topsecret.server.com']['forwardx11'])  #no
    17 print(config['DEFAULT']['serveraliveinterval'])   #45
    18 
    19 for key in config['bitbucket.org']:
    20     print('【bit】',key)
    21 
    22 print(config.options('bitbucket.org'))  # ??['user', 'serveraliveinterval', 'compression', 'compressionlevel']
    23 print(config.items('bitbucket.org'))  #[('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('user', 'hg')]
    24 ##  下面的两组,不管取哪个,都会把[DEFAULT] 里面的顺带打印出来,这以后在实际中有相应的应用、
    25 
    26 print(config.get('bitbucket.org','compression'))   #?? yes

    3、增、改、删

    1 ##------> 删、改、增
    2 config.add_section('zhao')   #  增加一条
    3 config.set('zhao','key','python')
    4 
    5 config.remove_section('topsecret.server.com')
    6 config.remove_option('bitbucket.org','user')
    7 
    8 config.write(open('zhao',"w"))
  • 相关阅读:
    服务器传输方式
    vs2010中设置环境变量方法
    乱语研发人员的安全感
    乱语研发人员的激情
    乱语员工发展之自由流动
    闲言碎语话心得什么时候轮到我
    一个企业没落,不是聪明人太多了,而是傻人太少了
    闲言碎语话心得凭什么是他(她)
    胡言乱语之客服人员
    选择指定表的字段名
  • 原文地址:https://www.cnblogs.com/jianguo221/p/9017157.html
Copyright © 2011-2022 走看看