python中configparser模块读取ini文件
python中configparser模块读取ini文件
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'
<br>
#第三种方法
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)<br>
#下面都会改变文件,所以最后一步都要重新写入配置文件
print('---------------------------增')
print('添加section:')
# config.add_section('key1')
print('添加键值对:')
# config.set('key1','k1','123456')
print('---------------------------改')
#如果需要修改配置文件里面的值,自行打开修改<br>
print('---------------------------删')
print('删除section:')
config.remove_section('key1')
print('删除键值对:')
config.remove_option('key1','k1')
#重新保存
config.write(open('confile','w'))
python中configparser模块读取ini文件的更多相关文章
- Python的ConfigParser模块读取ini配置文件 报错(持续更新总结)
1.ConfigParser.MissingSection什么的错误巴拉巴拉一堆,其实根本上就是没有读到配置文件,然后我去检查了一遍路径,发现没有问题,我是将文件的路径作为一个字符串拼接好传到另一个专 ...
- python中confIgparser模块学习
python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...
- Python中ConfigParser模块应用
Python中ConfigParser模块应用 Python的ConfigParser模块定义了3个对INI文件进行操作的类 RawConfigParser.ConfigParser和SafeConf ...
- Python使用ConfigParser模块读取配置文件(config.ini)以及写入配置文件
前言 使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是configParser.configPars ...
- python中configparser模块记录
python中用来读取配置文件,配置文件的格式相同于windows下的ini配置文件 一.常用函数 read(filename) #读取配置文件,直接读取ini文件内容 sections() #获取i ...
- python中configparser模块
python中的configparse模块的使用 主要用来解析一些常用的配置,比如数据配置等. 例如:有一个dbconfig.ini的文件 [section_db1] db = test_db1 ho ...
- python中configparser模块的使用
configparser模块用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 首先要写一个如下所示的配置文件: [DEFAULT] serv ...
- Python之xlrd模块读取xls文件与报错解决
安装 pip3 install xlrd 用法 Sheet编号从0开始 rows,colnum编号均从0开始 合并的单元格仅返回第一格内容 Sheets只能被调用一次,可获取所有sheet取idx 无 ...
- python封装configparser模块获取conf.ini值
configparser模块是python自带的从文件中获取固定格式参数的模块,因为是python只带的,大家用的应该很多,我觉得这个参数模块比较灵活,添加参数.修改参数.读取参数等都有对应的参数供用 ...
随机推荐
- yum -y install pip No package pip available. Error: Nothing to do
centos下安装pip时失败: [root@wfm ~]# yum -y install pipLoaded plugins: fastestmirror, refresh-packagekit, ...
- java 从 PKCS12(比如pfx格式)证书中提取私钥证书(PrivateKey)和受信任的公钥证书(X509Certificate)的序列号(SerialNumber)
import lombok.Cleanup; import lombok.Getter; import lombok.Setter; import lombok.SneakyThrows; impor ...
- 关于OpenGPU.org
今天是心情沉重的一天. OpenGPU.org,作为当年中国图形学界首屈一指的论坛,曾经创造过日访问破万的记录,而且汇聚了中国所有的图形行业的精英,大家畅所欲言,为整个中国图形学业界分享了无数宝贵的资 ...
- RabbitMQ安装后无法访问15672端口
切换到RabbitMQ的安装目录 sbin 目录下执行: rabbitmq-plugins enable rabbitmq_management 即可打开管理界面. rabbitmq的web管理界面无 ...
- vue create xxx 报错
在创建项目的时候,无论是默认配置还是自定义的配置,都会出现如下报错: 不用说,看项目中也只有一个packgge.json文件,项目必然是没有创建成功. 查看淘宝镜像 npm config get re ...
- Elasticsearch6.2.1安装elasticsearch-sq插件
参考 https://github.com/NLPchina/elasticsearch-sql 1.下载插件 wget https://github.com/NLPchina/elasticsear ...
- selenium===使用docker搭建selenium分布式测试环境
准备: #请在此之前先了解,selenium grid :参考:selenium-grid ,下载地址,win-本地部署过程 >>>环境准备: Linux操作系统 >>& ...
- Python - importlib 模块
importlib 模块可以根据字符串来导入相应的模块 目录结构: 在根目录下创建 importlib_test.py 和 aaa/bbb.py bbb.py: class Person(object ...
- Vue NGINX Apache 404 问题解决
location ^~/html/dist { #alias /home/server/webapps/vuejs-admin/; index index.html; try_files $uri $ ...
- [转]How to Install Oracle Java 11 in Ubuntu 18.04/18.10
链接地址:http://ubuntuhandbook.org/index.php/2018/11/how-to-install-oracle-java-11-in-ubuntu-18-04-18-10 ...