python中confIgparser模块学习
python中configparser模块学习
ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section), 每个节可以有多个参数(键=值)。使用的配置文件的好处就是不用在程序员写死,可以使程序更灵活。
目录
三种创建方法
增删改查
三种创建方法
程序示例:
import configparser #实例化出来一个类,相当于生成一个空字典
config = configparser.ConfigParser() #创建也很简单,键:值
# 值:键---值
#第一种方法
config['default']={'IP':'192.168.14.2',
'PORT':'6072'
} #第二种方法
config['Custom']={}
config['Custom']['User']='admin'
config['Custom']['Password']='123456'
#第三种方法
config['define']={}
Config=config['define']
Config['Host']='192.168.14.2'
Config['Port']='611' with open('confile','w') as configfile:
#注意这里,是谁调用write方法,是config对象,不是文件对象
config.write(configfile)
运行结果:
[default]
ip = 192.168.14.2
port = 6072 [Custom]
user = admin
password = 123456 [define]
host = 192.168.14.2
port = 611
增删改查
import configparser
config = configparser.ConfigParser()
#读取配置文件
config.read('confile')
print('获取文件内所有的section:')
print(config.sections()) print('获得指定section下所有option:')
options=config.options('Custom')
print(options) print('---------------------------查')
print('获取指定option下的值:')
value1=config['Custom']['user']
print(value1)
value2=config.get('Custom','user')
print(value2)
# getint(section,option) 得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat() 函数。 print('获取指定section下所有的键值对:')
items = config.items('default')
print(items) print('遍历键值对:')
for key in config['default']:
print(key) #下面都会改变文件,所以最后一步都要重新写入配置文件 print('---------------------------增') print('添加section:')
# config.add_section('key1') print('添加键值对:')
# config.set('key1','k1','123456') print('---------------------------改')
#如果需要修改配置文件里面的值,自行打开修改
print('---------------------------删') print('删除section:')
config.remove_section('key1') print('删除键值对:')
config.remove_option('key1','k1') #重新保存
config.write(open('confile','w'))
python中confIgparser模块学习的更多相关文章
- Python中ConfigParser模块应用
Python中ConfigParser模块应用 Python的ConfigParser模块定义了3个对INI文件进行操作的类 RawConfigParser.ConfigParser和SafeConf ...
- python中configparser模块读取ini文件
python中configparser模块读取ini文件 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(se ...
- python中configparser模块
python中的configparse模块的使用 主要用来解析一些常用的配置,比如数据配置等. 例如:有一个dbconfig.ini的文件 [section_db1] db = test_db1 ho ...
- python中configparser模块的使用
configparser模块用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 首先要写一个如下所示的配置文件: [DEFAULT] serv ...
- python 之ConfigParser模块学习
1.1 读取配置文件 -read(filename) 直接读取ini文件内容 -sections() 得到所有的section,并以列表的形式返回 -options(section) 得到该secti ...
- python中configparser模块记录
python中用来读取配置文件,配置文件的格式相同于windows下的ini配置文件 一.常用函数 read(filename) #读取配置文件,直接读取ini文件内容 sections() #获取i ...
- Python中optionParser模块的使用方法[转]
本文以实例形式较为详尽的讲述了Python中optionParser模块的使用方法,对于深入学习Python有很好的借鉴价值.分享给大家供大家参考之用.具体分析如下: 一般来说,Python中有两个内 ...
- Python中的模块介绍和使用
在Python中有一个概念叫做模块(module),这个和C语言中的头文件以及Java中的包很类似,比如在Python中要调用sqrt函数,必须用import关键字引入math这个模块,下面就来了解一 ...
- python封装configparser模块获取conf.ini值(优化版)
昨天晚上封装了configparser模块,是根据keyname获取的value.python封装configparser模块获取conf.ini值 我原本是想通过config.ini文件中的sect ...
随机推荐
- 第16月第5天 performSelector afterDelay cancel dispatch_semaphore_wait
1. //不延时,可能会导致界面黑屏并卡住一会 [self performSelector:@selector(startScan) withObject:nil afterDelay:0.3]; - ...
- python - 自定制property/property的延时计算
自定制prooerty: #模拟@property 实现将类的函数属性变成类属性: #定义描述符 class msf(): def __init__(self,obj): self.obj = obj ...
- Latex 公式颜色
如何给这个公式加上颜色? 解决方法: \usepackage{xcolor} \begin{align} \textcolor{red}{\int_a^b}\textcolor{blue}{f(x ...
- Check Box、Radio Button、Combo Box控件使用
Check Box.Radio Button.Combo Box控件使用 使用控件的方法 1.拖动控件到对话框 2. 定义控件对应的变量(值变量或者控件变量) 3.响应控件各种消息 Check Box ...
- Windows 10 的一些快捷键
Win键 + Q: 呼出[Cortana] Win键 + W:呼出[Windows INNK 工作区] Win键 + E: 呼出[资源管理器] Win键 + R: 呼出[运行] Win键 + A: 呼 ...
- vue系列之MVVM框架
当数据发生变化时,ViewModel就会检测到,然后通知相应的View改变 当用户操作View时,ViewModel就会检测到,然后Model,修改相应的数据,最终实现双向绑定 适用场景:针对具有复杂 ...
- NodeJs>------->>第二章:Node.js中交互式运行环境--------REL
第二章:Node.js中交互式运行环境--------REL 一:REPL运行环境概述 C:\Users\junliu>node > foo = 'bar' ; 'bar' > 二: ...
- 5个php实例,细致说明传值与传引用的区别
传值:是把实参的值赋值给行参 ,那么对行参的修改,不会影响实参的值 传引用 :真正的以地址的方式传递参数传递以后,行参和实参都是同一个对象,只是他们名字不同而已对行参的修改将影响实参的值 说明: 传值 ...
- window 连linux
https://blog.csdn.net/ruanjianruanjianruan/article/details/46954681 https://blog.csdn.net/u013754317 ...
- hdu2888 二维ST表(RMQ)
二维RMQ其实和一维差不太多,但是dp时要用四维 /* 二维rmq */ #include<iostream> #include<cstring> #include<cs ...