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"))。

转载于:https://www.cnblogs.com/gangzi4321/p/10943635.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值