python--ConfigParser读写改配置文件
2017-11-16 19:05
495 查看
from configparser import ConfigParser fp = 'conf.ini' #定义配置文件名 conf = ConfigParser() #实例化 conf.read(fp) # 打开conf conf.add_section('Section1') #添加conf节点 conf.set('Section1', 'name', 'jack') #添加值 conf.set('Section1', 'age', '23') conf.set('Section1', 'worker', 'CEO') conf.add_section('Section2') #添加conf节点 conf.set('Section2', 'name', 'rose') #添加值 conf.set('Section2', 'age', '21') conf.set('Section2', 'worker', 'CCC') with open(fp, 'w') as fw: #循环写入 conf.write(fw) ''' [Section1] name = jack age = 23 worker = CEO ''' #读取配置文件 from configparser import ConfigParser fp = 'conf.ini' #定义配置文件名 conf = ConfigParser() #实例化 conf.read(fp) # 打开conf name = conf.get('Section1','name') print(name) ''' 1)读取配置文件 read(filename) 直接读取ini文件内容 sections() 得到所有的section,并以列表的形式返回 options(section) 得到该section的所有option items(section) 得到该section的所有键值对 get(section,option) 得到section中option的值,返回为string类型 getint(section,option) 得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat() 函数。 ''' section= conf.sections() print(section) option =conf.options('Section1') print(option) item=conf.items('Section1') print(item) #改写操作 conf.set('Section1', 'name', 'jackadam') #设置为新值 with open(fp, 'w') as fw: #循环写入 conf.write(fw) from configparser import ConfigParser #重新读取 fp = 'conf.ini' #定义配置文件名 conf = ConfigParser() #实例化 conf.read(fp) # 打开conf name = conf.get('Section1','name') print(name)
相关文章推荐
- Python - ConfigParser模块读写配置文件
- Python模块之ConfigParser - 读写配置文件
- Python自动化测试 (二) ConfigParser模块读写配置文件
- 用ConfigParser模块读写配置文件——Python
- 【python】configparser读写配置文件
- Python模块之ConfigParser - 读写配置文件
- python3 configparser对配置文件读写
- Python自动化测试 (二) ConfigParser模块读写配置文件
- Python模块之ConfigParser - 读写配置文件
- python读写配置文件ConfigParser
- Python使用自带的ConfigParser模块读写ini配置文件
- Python模块之ConfigParser - 读写配置文件
- Python使用自带的ConfigParser模块读写ini配置文件
- Python自动化测试 -ConfigParser模块读写配置文件
- python 中对配置文件的解析 ConfigParser & ConfigObj
- Python中利用ConfigParser操作配置文件
- Python模块:配置文件解析器configparser
- python ConfigParser 配置读写
- Python模块之: ConfigParser 配置文件读取
- python中用Configparser模块解析配置文件