• python3 configparser 基本操作


    code

    import  configparser
     
    config = configparser.ConfigParser()
    file = 'config.ini'
    
    #添加section
    config.read(file)
    config.add_section('login')
    config.add_section('index')
    
    #添加option
    config.set('login','username','1111')
    config.set('login','password','2222')
    with open(file,'w+') as configfile:
        config.write(configfile)
    
    
    #是否存在section
    test1 = config.has_section("index")
    print(test1)
    #是否存在option
    test2 = config.has_option('login','username')
    print(test2)
    
    
    #返回所有可用的section
    section_list = config.sections()
    print(section_list)
    
    
    #返回指定section下的所有可用的option
    option_list = config.options("login")
    print(option_list)
    
    
    #读取option的值
    config.read(file)
    username = config.get('login','username')
    password = config.get('login','password')
    print(username,password)
    
    
    #移除option
    config.remove_option('login','username')
    with open(file,'w+') as configfile:
        config.write(configfile)
    
    
    #移除section
    config.remove_section('login')
    with open(file,'w+') as configfile:
        config.write(configfile)

  • 相关阅读:
    51Nod 1239 欧拉函数之和
    51Nod 1244 莫比乌斯函数之和
    BZOJ 4805: 欧拉函数求和
    BZOJ 3944: Sum
    3.25阅读摘抄
    生活整洁之道
    1064. 朋友数(20)
    1063. 计算谱半径(20)
    1061. 判断题(15)
    1062. 最简分数(20)
  • 原文地址:https://www.cnblogs.com/sea-stream/p/14079159.html
Copyright © 2020-2023  润新知