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 ...
随机推荐
- STATA一小步 我的一大步
第一次用STATA,以前一直搞SPSS,简直是生产力革命啊. 记下写的第一个命令 也是实现了一个probit回归 clear cd C:\Users\Qian\Desktop\1 insheet us ...
- 基于c#的windows基础设计(学习日记2)【关于多态】
这次的多态很简单,没什么知识点 . 直接贴代码了: public abstract class Animal //建立一个抽象类 { private bool m_sex; private strin ...
- ThinkPHP5.0源码学习之框架启动流程
ThinkPHP5框架的启动流程图如下: ThinkPHP5的启动流程按照文件分为三步: 1.请求入口(public/index.php) 2.框架启动(thinkphp/start.php) 3.应 ...
- STL 小白学习(4) deque
#include <iostream> #include <deque> //deque容器 双口 using namespace std; void printDeque(d ...
- two week summary
from collections import Iteratorfrom collections import Iterabl dic = {'a':"a","91a&q ...
- Win10系列:C#应用控件基础8
ToggleSwitch控件 在应用程序中ToggleSwitch控件可以模拟一个允许用户在启用和禁用两种状态之间进行切换的物理开关,ToggleSwitch控件的功能与我们在日常生活中所使用的电源开 ...
- 汇编语言debug入门
进入windows操作系统,因为我的虚拟机用的是win7 64位,所以装了一个Dos Box 的软件来执行这些指令. 输入debug回车,这样就进入了debug模式. 1: 输入 -r 查看或者修改寄 ...
- Hibernate----面试题
什么是Hibernate? hibernate是一个基于ORM持久框架,可以让程序员以面向对象的思想操作数据库,提高生产效率. 什么是ORM? orm不过是一种思想,对象关系映射.是对象关系模型,如h ...
- 数据库编程加入transaction
TransManager tm = new TransManager(); tm.begin();//开启事物 try { //sql执行代码 // // tm.commit();//更改完sql之后 ...
- Linux修改日期、时间,系统与硬件时间
Linux的时间分为两种,硬件时间和系统时间两种: 一.查看与修改系统时间 查看系统时间:date # date Fri Nov 26 15:20:18 CST 1999 用指定的格式显示系统时间: ...