• python(configparse)


    configparse------配置文件解析模块
    创建一个配置文件
    # encoding:utf-8
    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'

    对创建的配置文件增删改查

    example.ini文件信息

    # encoding:utf-8
    import configparser
    
    import configparser
    
    config = configparser.ConfigParser()
    
    #---------------------------------------------查
    print(config.sections())   #[]
    
    config.read('example.ini')  #读文件
    
    print(config.sections())   #打印块名
    
    print('bytebong.com' in config) # 判断是否存在
    
    print(config['bitbucket.org']['User'])      # hg
    
    print(config['DEFAULT']['Compression'])     # 判断是否存在
    
    print(config['topsecret.server.com']['ForwardX11'])  #no
    
    #遍历字典
    for key in config['bitbucket.org']:
        print(key)
    
    #结果如下
    # user
    # serveraliveinterval
    # compression
    # compressionlevel
    # forwardx11
    
    
    print(config.options('bitbucket.org'))#['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11']
    print(config.items('bitbucket.org'))  #[('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'hg')]
    
    print(config.get('bitbucket.org','compression'))#yes
    
    #---------------------------------------------删,改,增(config.write(open('i.cfg', "w")))
    
    
    config.add_section('yuan')      #
    
    config.remove_section('topsecret.server.com')       #删块
    config.remove_option('bitbucket.org','user')        #删块下面信息
    
    config.set('bitbucket.org','k1','11111')            #
    
    config.write(open('i.cfg', "w"))                        #文件写入,覆盖原文件
  • 相关阅读:
    ListView的item中EditText编辑(或者其他控件)修改本行数据
    C#:MVC引用Log4Net生成错误日志
    Web Developer教程
    EditPlus高级使用技巧(附视频、课件、代码下载)
    jQuery入门篇
    网摘系统架构
    BugFree 2.0使用帮助
    使用 WebDeployment Project 视频
    BugFreeHelper 2.2 For BugFree2.0(RTM)
    FireFox3推荐安装附加组件Top10(附官方主页和下载地址)
  • 原文地址:https://www.cnblogs.com/2018-1025/p/11055142.html
Copyright © 2020-2023  润新知