• 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配置文件

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

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

在用Python做开发的时候经常会用到数据库或者其他需要动态配置的东西,硬编码在里面每次去改会很麻烦。Python自带有读取配置文件的模块ConfigParser,使用起来非常方便。

ini文件
ini配置文件格式:

2016626174140238.png (273×249)

读取配置文件:

import ConfigParser
conf = ConfigParser.ConfigParser()
conf.read('dbconf.ini')       # 文件路径
name = conf.get("section1", "name") # 获取指定section 的option值
print name
sex = conf.get("section1", "sex")  # 获取section1 的sex值
print age

</div>

输出:

jhao
male
</div>


写入配置文件:

import ConfigParser
conf = ConfigParser.ConfigParser()
conf.read('dbconf.ini')

conf.set("section1", "name", "jhao104")    # 修改指定section 的option
conf.set("section1", "age", "21")       # 增加指定section 的option
conf.add_section("section3")         # 增加section
conf.set("section3", "site", "oschina.net")  # 给新增的section 写入option
conf.write(open('dbconf.ini', 'w'))

</div>

输出:

2016626174242767.png (288×355)

</div>

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

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

相关文章

  • Python定时执行之Timer用法示例
  • Python的函数的一些高阶特性
  • Python实现将目录中TXT合并成一个大TXT文件的方法
  • 基于python实现微信模板消息
  • Python使用SocketServer模块编写基本服务器程序的教程
  • python根据距离和时长计算配速示例
  • 微信 用脚本查看是否被微信好友删除
  • python解决方案:WindowsError: [Error 2]
  • python基于urllib实现按照百度音乐分类下载mp3的方法
  • 详解Django通用视图中的函数包装

文章分类

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

最近更新的内容

    • Python struct.unpack
    • Python 类的继承实例详解
    • 在Python中使用NLTK库实现对词干的提取的教程
    • python分析网页上所有超链接的方法
    • 将Python的Django框架与认证系统整合的方法
    • 用Python程序抓取网页的HTML信息的一个小实例
    • Python爬虫利用cookie实现模拟登陆实例详解
    • 解析Python中while true的使用
    • python实现RSA加密(解密)算法
    • 在Python的Django框架中simple-todo工具的简单使用

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

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