• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python中使用ConfigParser解析ini配置文件实例

Python中使用ConfigParser解析ini配置文件实例

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了python configparser,python中configparser,python3 configparser,configparser,import configparser等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

ini文件是windows中经常使用的配置文件,主要的格式为:
[Section1]
option1 : value1
option2 : value2
</div>
python提供了一个简单的模块ConfigParser可以用来解析类似这种形式的文件。对于ConfigParser模块可以解析key:value和key=value这样的类型,对于#和;开头的行将会自动忽视掉。相当于注释行。常用的函数:
ConfigParser.RawConfigParser()

RawConfigParser Object的操作有:

.sections() : 返回所有可用的section
.addsection(sectionname) :添加section
.set(sectionname, optionname, optionvalue): 添加option
.hassection(sectionname) :判断
.options(sectionname) : 返回section下可用的option
.hasoption(sectionname, optionname) : 判断
.read(filename) : 读取文件
.wrie(filename) : 将RawConfigParser对象写到文件中
.get(sectionname, optionname) : 获取值, 默认的是返回string类型
.getfloat, .getint, .getboolean : 获取不同类型的返回值,参数和get的参数一样
.items(sectionname) :列出section下的所有key:value
.remove(sectionname) :删除section
.remove(sectionname, option_name) : 删除section下的某个option
</div>
Demo -- 生成文件
$ cat ini_demo.py
# -*- coding:utf-8 -*-

import ConfigParser

def gen_ini():
    ftest = open('test','w')
    config_write = ConfigParser.RawConfigParser()
    config_write.add_section('Section_a')
    config_write.add_section('Section_b')
    config_write.add_section('Section_c')
    config_write.set('Section_a','option_a1','apple_a1')
    config_write.set('Section_a','option_a2','banana_a2')
    config_write.set('Section_b','option_b1','apple_b1')
    config_write.set('Section_b','option_b2','banana_b2')
    config_write.set('Section_c','option_c1','apple_c1')
    config_write.set('Section_c','option_c2','banana_c2')  
    config_write.write(ftest)
    ftest.close()

if __name__ == "__main__":
    gen_ini()
</div>
最后生成的文件为:
$ cat test
[Section_a]
option_a1 = apple_a1
option_a2 = banana_a2

[Section_c]
option_c2 = banana_c2
option_c1 = apple_c1

[Section_b]
option_b1 = apple_b1
option_b2 = banana_b2
Demo -- 读取文件

def read_ini():
    config_read = ConfigParser.RawConfigParser()
    config_read.read('test')
    print config_read.sections()
    print config_read.items('Section_a')
    print config_read.get('Section_a','option_a1')
</div>
最后的结果为:
['Section_a', 'Section_c', 'Section_b']
[('option_a2', 'banana_a2'), ('option_a1', 'apple_a1')]
apple_a1
</div>

</div>

您可能想查找下面的文章:

  • Python自动化测试ConfigParser模块读写配置文件
  • Python自动化测试ConfigParser模块读写配置文件
  • Python使用自带的ConfigParser模块读写ini配置文件
  • Python中的ConfigParser模块使用详解
  • Python中的ConfigParser模块使用详解
  • Python中使用ConfigParser解析ini配置文件实例
  • Python中使用ConfigParser解析ini配置文件实例
  • python解析模块(ConfigParser)使用方法
  • python解析模块(ConfigParser)使用方法

相关文章

  • 在Python的while循环中使用else以及循环嵌套的用法
  • python读取TXT到数组及列表去重后按原来顺序排序的方法
  • 完美解决Python2操作中文名文件乱码的问题
  • python集合用法实例分析
  • Python多线程爬虫简单示例
  • Python的print用法示例
  • Python的Tornado框架实现异步非阻塞访问数据库的示例
  • tensorflow estimator 使用hook实现finetune方式
  • Python的string模块中的Template类字符串模板用法
  • 用python记录运行pid,并在需要时kill掉它们的实例

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python常用算法学习基础教程
    • python实现识别相似图片小结
    • 使用Python编写一个简单的tic-tac-toe游戏的教程
    • python通过索引遍历列表的方法
    • Python的string模块中的Template类字符串模板用法
    • Pycharm学习教程(2) 代码风格
    • Python fileinput模块使用实例
    • python备份文件以及mysql数据库的脚本代码
    • 利用Python中unittest实现简单的单元测试实例详解
    • Python 可爱的大小写

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有