python configparser使用
.ini文件由若干section(部分)组成, 而每一个section又由若干键值对组成。
以 example.ini为例:
[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes [bitbucket.org]
User = hg [topsecret.server.com]
Port = 50022
ForwardX11 = no
创建.ini文件
import configparser # configd对象类似于字典
config = configparser.ConfigParser()
config["DEFAULT"] = {
"ServerAliveInterval" : 45,
"Comperssion": "yes",
"ComperssionLevel": 9,
"ForwardX11": "yes"
} config["bitbucket.org"] = {}
config["bitbucket.org"]["user"] = "hg" config["topsecret.server.com"] = {}
config["topsecret.server.com"]["Port"] = ""
config["topsecret.server.com"]["ForwardX11"] = "no" with open("example.ini", "w") as fp:
# 写入文件
config.write(fp)
读取.ini文件
config = configparser.ConfigParser()
config.read("example.ini")
关于section
import configparser config = configparser.ConfigParser()
config.read("example.ini")
# 获取section名称列表
print(config.sections()) # ['bitbucket.org', 'topsecret.server.com']
# 获取默认section名称
print(config.default_section) # DEFAULT
# 获取默认section对象
print(config.defaults()) # OrderedDict([('serveraliveinterval', '45'), ('comperssion', 'yes'), ('comperssionlevel', '9'), ('forwardx11', 'yes')]) # 判断是否包含对应的section
print('bitbucket.org' in config)
print(config.has_section("example.com")) # 添加section的2种方式
config['example.com'] = {}
config['example.com']['username'] = 'python'
config.add_section("example.com1") config.write(open("tmp.ini", "w")) # 删除section的2种方式
del config['example.com']
config.remove_section("example.com")
config.write(open("tmp.ini", "w"))
关于option
import configparser config = configparser.ConfigParser()
config.read("example.ini") # 这里的option于key对应
# 获取section对应的key列表
bitbucket_keys = config.options('bitbucket.org')
print(bitbucket_keys) # ['user', 'serveraliveinterval', 'comperssion', 'comperssionlevel', 'forwardx11'] # 判断section中是否存在对应的key
print(config.has_option("bitbucket.org", "User"))
print('user1' in config.options('bitbucket.org'))
print('user1' in config['bitbucket.org']) # 添加option
config['bitbucket.org']['pwd'] = 'python'
config.write(open("tmp.ini", "w")) # 删除option的2种方式
del config['bitbucket.org']['User']
config.remove_option("bitbucket.org", "User")
config.write(open("tmp.ini", "w"))
value相关
import configparser config = configparser.ConfigParser()
config.read("example.ini") # 获取key对应的value的2种方式
user = config['bitbucket.org']['User']
user = config.get('bitbucket.org', 'User')
print(user) # hg interval = config.getint("topsecret.server.com", "Port")
print(interval) #
compression = config.getboolean("topsecret.server.com", "ForwardX11")
print(compression)
# 还有 config.getfloat() # 设置value的2种方式
config.set('bitbucket.org', 'User', 'peter')
config['bitbucket.org']["User"] = "peter"
config.write(open("tmp.ini", "w"))
python configparser使用的更多相关文章
- python configparser模块
来看一个好多软件的常见文档格式如下: [DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 Forward ...
- python ConfigParser配置读写
一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号"[ ]"内包含的为section.section 下面为类似于key ...
- python ConfigParser、shutil、subprocess、ElementTree模块简解
ConfigParser 模块 一.ConfigParser简介ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.section 下面为类 ...
- [Python]ConfigParser解析配置文件
近期发现非常多接口配置都硬编码在souce file中了,于是就看了下python怎么解析配置文件,重构下这一块. 这个应该是早就要作的... 配置文件: [mysqld] user = mysql ...
- Python configparser 读取指定节点内容失败
# !/user/bin/python # -*- coding: utf-8 -*- import configparser # 生成一个config文件 config = configparser ...
- Python Configparser模块读取、写入配置文件
写代码中需要用到读取配置,最近在写python,记录一下. 如下,假设有这样的配置. [db] db_host=127.0.0.1 db_port=3306 db_user=root db_pass= ...
- Python ConfigParser的使用
1.基本的读取配置文件 -read(filename) 直接读取ini文件内容 -sections() 得到所有的section,并以列表的形式返回 -options(section) 得到该sect ...
- python ConfigParser读取配置文件,及解决报错(去掉BOM)ConfigParser.MissingSectionHeaderError: File contains no section headers的方法
先说一下在读取配置文件时报错的问题--ConfigParser.MissingSectionHeaderError: File contains no section headers 问题描述: 在练 ...
- 【转载】Python ConfigParser的使用
1.基本的读取配置文件-read(filename) 直接读取ini文件内容-sections() 得到所有的section,并以列表的形式返回-options(section) 得到该section ...
随机推荐
- Python---高级函数map, filter, zip, enumerate等的用法
今天看自然语言处理这本书的时候,被这里的高级函数的概念吸引了,因为我觉得所有的函数都只是函数而已,是为了实现特定功能而实现的,不应该有高级,低级之分啊!不过了解之后,发现这几个函数确实是有点高级,非常 ...
- quartz.net的使用
Quartz.NET简介 Quartz.NET作业调度框架是伟大组织OpenSymphony开发的quartz scheduler项目的.net延伸移植版本. Quartz.NET是一个开源的作业调度 ...
- SQL Update 语句详解
SQL Update 语句详解 Update 语句 Update 语句用于修改表中的数据. 语法: UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值 Person: L ...
- MinGW-w64安装教程——著名C/C++编译器GCC的Windows版本
本文主要讲述如何安装 C语言 编译器——MinGW-w64,特点是文章附有完整详细的实际安装过程截图,文字反而起说明提示作用. 编写本文的原因始于我的一个观点:图片可以比文字传达更多的信息,也能让其他 ...
- 第二周例行报告psp
此作业要求详见https://edu.cnblogs.com/campus/nenu/2018fall/homework/2127 (1)psp表 本周进度条 累计进度图 本周PSP饼状图
- c++中关于预编译头的设置问题
在运行代码时会遇到缺少预编译pch.c 或者stadfx.h之类的, 这个时候,先查看有没有包含, 然后看一下预编译头设置中, 是否正确设置.
- 理解微信小程序的生命周期和运行原理
写微信小程序,他的生命周期不能不知道,不知道小程序就会出现各种bug而无法解决.小助君公众号带你学习小程序的生命周期和运行原理. 小程序由两大线程组成:负责界面的线程(view thread)和服务线 ...
- python day27--常用模块 time,random,os,序列化
一.time模块 %y 两位数的年份表示(00-99) %Y 四位数的年份表示(000-9999) %m 月份(01-12) %d 月内中的一天(0-31) %H 24小时制小时数(0-23) %I ...
- Python之DataFrame常用方法小结
https://blog.csdn.net/a786150017/article/details/78573055
- JAVA高级篇(二、JVM内存模型、内存管理之第二篇)
本文转自https://zhuanlan.zhihu.com/p/25713880. JVM的基础概念 JVM的中文名称叫Java虚拟机,它是由软件技术模拟出计算机运行的一个虚拟的计算机. JVM也充 ...