configparser

  • import configparser
    config = configparser .ConfigParser ()
    config["DEFULT"] = {"a":"1",
                          "b":"2",
                          "c":"3"}
    config["dsadsad.org"] = {}
    config ["dsadsad.org"]["user"] = "hg"
    
    
    
    
    
    with open("example.ini","w") as f:
        config .write(f)

     

  • config.read("example.ini")
    print(config .sections() )  #块名
    print("dsadsad.org" in config ) #判断块是否在config中
    print(config ["DEFULT"]["A"])  #不区分大小写

     

  • for key in config ["dsadsad.org"]:     #user     default块中会遍历
                                              # a
                                              #b
        print(key)                            #c
    print(config.options("dsadsad.org")
     #遍历后放在列表中

     

  • print(config .items("dsadsad.org") )       #将键和值放在元组
    print(config.get("dsadsad.org","a")  )     #因为会遍历默认块,所以可以找到

     

  • config .add_section("yuan")              #增加块
    config .set("yuan","sdsa","asdsa")    #增加值
    config.remove_section("yuan")            #删除块
    config .remove_option("yuan","sdsa")         #删除值
    config .write(open("i.cfg","w") )

     

posted on 2018-02-25 21:37  python_an  阅读(144)  评论(0编辑  收藏  举报

导航