• 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编程中对文件和存储器的读写示例
  • 精确查找PHP WEBSHELL木马的方法(1)
  • Python的Bottle框架中实现最基本的get和post的方法的教程
  • python根据京东商品url获取产品价格
  • Python操作sqlite3快速、安全插入数据(防注入)的实例
  • 跟老齐学Python之开始真正编程
  • 在Python中使用dict和set方法的教程
  • 优化Python代码使其加快作用域内的查找
  • 关于tf.reverse_sequence()简述
  • python fabric实现远程部署

文章分类

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

最近更新的内容

    • Django卸载之后重新安装的方法
    • python获取当前用户的主目录路径方法(推荐)
    • django1.8使用表单上传文件的实现方法
    • Python简单计算文件夹大小的方法
    • Python 基础知识之字符串处理
    • Python实现求最大公约数及判断素数的方法
    • python实现简单爬虫功能的示例
    • 全面了解Python的getattr(),setattr(),delattr(),hasattr()
    • python实现清屏的方法
    • 处理Python中的URLError异常的方法

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

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