【Python】配置文件读取
来源:互联网 发布:免费的视频会议软件 编辑:程序博客网 时间:2024/06/03 09:30
Python提供了ConfigParser包来进行配置文件读取。
API:
- read(filename) 读取ini文件内容
- sections() 得到所有的section,并以list的形式返回
- options(section) 得到该section的所有option
- items(section) 得到该section的所有键值对
- get(section,option) 得到section中option的值,返回为string类型
- getint(section,option) 得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat() 函数
- add_section(section) 添加一个新的section
- set( section, option, value) 对section中的option进行设置,需要调用write将内容写入配置文件
所谓的sections就是一个配置块。下面是一个配置文件db.ini
[mongo]db_host = 127.0.0.1db_port = 27017[system]timeout = 2000interval = 2000
包含mongo和system两个配置块。
配置文件读取:
import configparser as CPimport stringimport osimport syscf = CP.ConfigParser()cf.read("db.ini")# 获取所有的sectionsecs = cf.sections()# 遍历sectionsfor sec in secs: print("\nsection [" + sec + "]:") # 获取section里的所有配置项 options = cf.options(sec) for o in options: print(o+"="+cf.get(sec, o))
结果展示:
读取db_host这一配置项:
print(cf.get("mongo", "db_host"))
0 0
- Python读取ini配置文件
- Python读取配置文件
- python读取配置文件
- 用Python读取配置文件
- Python读取ini配置文件
- python读取配置文件
- python 读取配置文件
- python读取properties配置文件
- Python读取配置文件
- 【Python】配置文件读取
- Python读取配置文件
- python 读取配置文件
- Python读取INI配置文件
- Python 读取配置文件
- python读取配置文件
- python 读取配置文件
- python读取配置文件configparser
- python读取配置文件
- Android屏幕适配(整理)
- ViewPager + Fragment的预加载解决方法
- linux-内核概述学习笔记
- 文章标题
- android数字签名
- 【Python】配置文件读取
- 今天在土豆听了一张好听的音乐串烧
- shell编程(特别概念五)
- php
- 那些年,我了解过的医疗器械(偏影像方向)公司 (2017.03.24 Updating...)
- Android PKMS拦截adb安装应用
- vs中使用my97日历控件
- arcgis api for js入门开发系列三地图工具栏(含源代码)
- windows7操作系统安装步骤(精简版)