• Python之路——configparser模块


    configparser模块

    用python生成一个config文档

     1 config = configparser.ConfigParser()    # 创建一个configparser对象
     2 config['DEFAULT'] = {
     3                         'ServerAliveInterval': '45',
     4                         'Compression': 'yes',
     5                         'CompressionLevel': '9'
     6                         }
     7 config['bitbucket.org'] = {}
     8 config['bitbucket.org']['User'] = 'hg'
     9 config['topsecret.server.com'] = {}
    10 topsecret = config['topsecret.server.com']
    11 topsecret['Host Port'] = '50022'
    12 topsecret['ForwardX11'] = 'no'
    13 config['DEFAULT']['ForwardX11'] = 'yes'
    14 with open('example.ini','w') as configfile:
    15     config.write(configfile)

     增删改查

     1 # 增删改查
     2 # config = configparser.ConfigParser()
     3 # print(config.sections())# []
     4 # config.read('example.ini')
     5 # print(config.sections())
     6 
     7 # for k in config.sections():
     8 #     print(k)
     9 
    10 # for k,v in config['bitbucket.org'].items():
    11 #     print(k,v)
    12 
    13 # if 'user' in config['bitbucket.org']:
    14 #     print('in')
    15 
    16 # print(config['bitbucket.org']['user'])
    17 
    18 # print(dir(config))
    19 
    20 # print(config.options('topsecret.server.com'))
    21 
    22 # print(config['topsecret.server.com']['k1'])
    23 
    24 # config['topsecret.server.com']['name'] = '22'
    25 # config['topsecret.server.com']['age'] = '22'
    26 # config.write(open('example2.ini','w'))
    27 
    28 # config.set('topsecret.server.com','host port','456798')
    29 # config.write(open('example3.ini','w'))
    30 
    31 # config.remove_option('topsecret.server.com','host port')
    32 # config.write(open('example4.ini','w'))
  • 相关阅读:
    BZOJ4892: [Tjoi2017]dna
    BZOJ4307: Maishroom & Class
    NCEE2018游记
    BZOJ3720: Gty的妹子树
    BZOJ5055: 膜法师
    「奇技淫巧」博客园页面美化(差不多是划水
    白痴qwerta的胡言乱语(一句话日度感想?
    关于qwerta
    NOIP2018退役记
    「NOIP2017」「LuoguP3952」 时间复杂度(模拟,栈
  • 原文地址:https://www.cnblogs.com/liuyankui163/p/8376700.html
Copyright © 2020-2023  润新知