python文件操作之ini文件

0x 01ini文件的格式

[student]   //SECTION是一个节的名称
name = gumin   //section下面是键值对的组合
sex = man
birthday = 1990

[blonging]
phone = iphone10s
computer = thinkpad

0x02  ConfigParser模块

ConfigParser 是用来读取配置文件的模块,下面我们通过代码示例来看一下它是如何使用的

#-*-coding:utf-8-*-
import ConfigParser #引入ConfigParser模块
cfg = Config.Parser.ConfigParser #初始化ConfigParser
cfg.read("Config.ini") #导入测试的ini文件,参考0x00
cfg.section() #输出section列表
<img src="https://img-blog.csdn.net/20151025162245463" alt="" />
cfg.items('student')  #输出student节下的所有键值对列表
<img src="https://img-blog.csdn.net/20151025162348165" alt="" />
cfg.options('student') #输出student节下所有key的值
<img src="https://img-blog.csdn.net/20151025164205070" alt="" />
cfg.get('student','name') #获取student节下的key值为name的value
<img src="https://img-blog.csdn.net/20151025164354609" alt="" />
cfg.set('student','name','gumin2') #修改name键对应的值,或者你新插入键值对,只要保证你的key是不重复的
cfg.remove_option('student','sex') #删除sex键

0x03 Config文件管理类


待补充





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值