zoukankan      html  css  js  c++  java
  • Python configparser模块

    import configparser
    
    conf = configparser.ConfigParser()
    conf.read("test.cfg")
    sections = conf.sections()
    print (sections)

    ['sec_c', 'sec_b', 'sec_a']
    options = conf.options("sec_a") print (options) kvs = conf.items("sec_a") print(kvs)
    ['a_key1', 'a_key2']
    str_val = conf.get("sec_a","a_key1")
    print (type(str_val))
    print (str_val)
    <class 'str'>
    20
    int_val = conf.getint("sec_a","a_key2")
    print (type(int_val))
    print (int_val)
    <class 'int'>
    10
    
    conf.set("sec_b","b_key3","$new-$r")
    conf.write(open("test.cfg","w"))

    修改[sec_a]段中[b_key3]的值为"$new-$r" conf.set(
    "sec_b","b_newkey","new_value") conf.write(open("test.cfg","w"))
    在[sec_b]中先加了一个option conf.add_section(
    "sec_d") conf.set("sec_d","new_d_key","new_d_value") conf.write(open("test.cfg","w")) 新增加一个section
  • 相关阅读:
    1002 写出这个数
    1001 害死人不偿命的(3n+1)猜想
    Graph I
    Tree
    进程通信
    管道
    fork函数
    Priority Queue
    Search
    游戏 slider
  • 原文地址:https://www.cnblogs.com/python-study/p/5555727.html
Copyright © 2011-2022 走看看