Python基础七--configparser模块

一、configparser 

  a. 读

  1. 实例化:configparser_handle = configparser.ConfigParser();

  2. 读入:configparser_handle.read("config_document.cfg");

  3. 获取标题:configparser_handle.sections();

  4. 获取标题下列表:item_list = configparser_handle.items('term')

  5.  获取字符值方法:configparser_handle.get(‘term’,‘name’);

  6. 获取整数值方法:configparser_handle..getint(‘term’,‘number’);

  7. 获取布尔值方法:configparser_handle..getboolean(‘term’,‘number’);

  8. 获取浮点值方法:configparser_handle..getfloat(‘term’,‘salary’);

  b. 写

  1. 实例化:configparser_handle = configparser.ConfigParser();

  2. 读入:configparser_handle.read("config_document.cfg",encoding = "utf-8);

  3. 删除整块信息:configparser_handle.remove_section("section_test_one");

  4. 删除块信息内的指定信息:configparser_handle.remove_option("section_test_two","key_three");

  5. 判断是否存在"section_test_one":configparser_handle.has_section("section_test_one");

  6. 判断是否存在"section_test_one"下“key_one”:configparser_handle.has_section("section_test_one","key_one");

  7. 添加section:configparser_handle.add_section("section_new");

  8. 在'section_new'中添加"key_one='gangzi':configparser_handle.set("section_new","key_one","gangzi");

  9. 最后将写的内容写入文件,完成最终写:configparser_handle.write(open("config_document.write","w"))。

猜你喜欢

转载自www.cnblogs.com/gangzi4321/p/10943635.html