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只带的,大家用的应该很多,我觉得这个参数模块比较灵活,添加参数.修改参数.读取参数等都有对应的参数供用 ...
随机推荐
- Zookeeper:Unable to read additional data from client sessionid 0x00, likely client has closed socket
异常信息: 2018-03-20 23:34:01,887 [myid:99] - INFO [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerC ...
- (转)Ngx_Lua使用分享
原文:https://www.cnblogs.com/yanzi-meng/p/9450999.html ngx_lua 模块详细讲解(基于openresty)---https://www.cnblo ...
- CentOS6非root用户下安装及配置CDH5.3.0
#install lsb packagesudo yum install -y redhat-lsb #install net-tools package sudo yum install -y ne ...
- Maven依赖包导入错误(IntelliJ IDEA):java.lang.OutOfMemoryError: GC overhead limit exceeded
一.问题背景 最近用IntelliJ IDEA 打开一个老应用,一直加载依赖不成功,主POM中存在如下错误. java.lang.OutOfMemoryError:GC overhead limit ...
- python2与python3的区别齐全【整理】
本文链接:https://blog.csdn.net/pangzhaowen/article/details/80650478 展开 一.核心类差异1. Python3 对 Unicode 字符的原生 ...
- MySQL数据类型:UNSIGNED注意事项(转)
原文地址:https://www.cnblogs.com/blankqdb/archive/2012/11/03/blank_qdb.html 1. UNSIGNED UNSIGNED属性就是将数字类 ...
- SpringMVC返回值响应
1.响应数据和结果视图 1.1 搭建环境 New Module -> Module SDK 1.8 -> Create from archetype -> maven-archety ...
- 【Spring Boot学习之六】Spring Boot整合定时任务&异步调用
环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2一.定时任务1.启动类添加注解@EnableScheduling 用于开启定时任务 package com.wjy; i ...
- Modelsim SE-64 10.4的安装 、破解以及远程使用
1.准备好modelsim SE-64 10.4的安装包和破解文件(modelsim-win64-10.4-se.exe 和MentorKG.exe ,patch_dll.bat). 2.安装 好 ...
- [QT] - MjpegStreamer客户端(简易版)#工程源码
简介: 大学时期学习弄的一个小软件,可以起到示例的作用,软件的几个功能截图如正文所示,文末提供工程源码文件,感谢支持! 功能截图: [ 开发板启动 mjpg_streamer 服务器 ] [ 启动软件 ...