Python全站之路----常用模块----configparser模块
config:配置 parser:解析
此模块用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser,在 python 2.x 里名字为 ConfigParer
来看一个好多软件的常见配置文件格式如下
```cnf
[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes [bitbucket.org]
User = hg [topsecret.server.com]
Port = 50022
ForwardX11 = no
```
解析
```py
>>> import configparser # 导入模块
>>> config = configparser.ConfigParser() #实例化(生成对象)
>>> config.sections() #调用sections方法
[]
>>> config.read('example.ini') # 读配置文件(注意文件路径)
['example.ini']
>>> config.sections() #调用sections方法(默认不会读取default)
['bitbucket.org', 'topsecret.server.com']
>>> 'bitbucket.org' in config #判断元素是否在sections列表内
True
>>> 'bytebong.com' in config
False
>>> config['bitbucket.org']['User'] # 通过字典的形式取值
'hg'
>>> config['DEFAULT']['Compression']
'yes'
>>> topsecret = config['topsecret.server.com']
>>> topsecret['ForwardX11']
'no'
>>> topsecret['Port']
''
>>> for key in config['bitbucket.org']: print(key) # for循环 bitbucket.org 字典的key
...
user
compressionlevel
serveraliveinterval
compression
forwardx11
>>> config['bitbucket.org']['ForwardX11']
'yes'
```
其他增删改查语法
import configparser config = configparser.ConfigParser()
config.read('conf_test.ini') ########## 读 ##########
secs = config.sections() #获取结点的值
print(secs)
options = config.options('group2') # 获取指定section的keys
print(options) item_list = config.items('group2') # 以元组的形式获取指定 section 的 keys & values ,key value
print(item_list) val = config.get('group1','key') # 获取指定的key 的value
val = config.getint('group1','key') # 获取指定key 的value ,其中value必须为 int 类型 ########## 改写 ##########
sec = config.remove_section('group1') # 删除section 并返回状态(true, false)
config.write(open('conf_test.ini', "w")) # 每个对应的修改操作都要写入文件才会生效 sec = config.has_section('wupeiqi') #section 中是否存在 wupeiqi
sec = config.add_section('wupeiqi') #添加section
config.write(open('conf_test.ini', "w")) config.set('group2','k1','') #向section中添加key及其对应value,其中key和value都必须是str
config.write(open('conf_test.ini', "w")) config.remove_option('group2','age') #删除指定section中的指定key及其对应value值
config.write(open('conf_test.ini', "w"))
Python全站之路----常用模块----configparser模块的更多相关文章
- python day 9: xlm模块,configparser模块,shutil模块,subprocess模块,logging模块,迭代器与生成器,反射
目录 python day 9 1. xml模块 1.1 初识xml 1.2 遍历xml文档的指定节点 1.3 通过python手工创建xml文档 1.4 创建节点的两种方式 1.5 总结 2. co ...
- Python3学习之路~5.11 configparser模块
用于生成和修改常见配置文档,当前模块的名称在 python 2.x 版本中为 ConfigParser, python 3.x 版本中变更为 configparser. 来看一个好多软件的常见文档格式 ...
- python学习第五十三天configParser模块的使用
configParser 模块用于生成和修改常见配置文档,python 3.x为configParser,配置软件的常见配置格式 模块的用法 import configparser config=co ...
- Python第十一章-常用的核心模块01-collections模块
python 自称 "Batteries included"(自带电池, 自备干粮?), 就是因为他提供了很多内置的模块, 使用这些模块无需安装和配置即可使用. 本章主要介绍 py ...
- Python第十一章-常用的核心模块03-json模块
python 自称 "Batteries included"(自带电池, 自备干粮?), 就是因为他提供了很多内置的模块, 使用这些模块无需安装和配置即可使用. 本章主要介绍 py ...
- Python第十一章-常用的核心模块04-datetime模块
python 自称 "Batteries included"(自带电池, 自备干粮?), 就是因为他提供了很多内置的模块, 使用这些模块无需安装和配置即可使用. 本章主要介绍 py ...
- python之shelve、xml、configparser模块
一.shelve模块 shelve模块比pickle模块简单,只有一个open函数,返回类似字典的对象,可读可写;key必须为字符串,而值可以是python所支持的数据类型 import shelve ...
- python学习之路-第四天-模块
模块 sys模块 sys.argv:参数列表,'using_sys.py'是sys.argv[0].'we'是sys.argv[1].'are'是sys.argv[2]以及'arguments'是sy ...
- 常用模块(collections模块,时间模块,random模块,os模块,sys模块,序列化模块,re模块,hashlib模块,configparser模块,logging模块)
认识模块 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写的 ...
随机推荐
- Java获取本地IP地址和主机名
方式一:通过java.net.InetAddress类获取 public void test1() { try { InetAddress addr = InetAddress.getLocalHos ...
- JS-圣杯模式
var inherit = (function(){ var F = function(){}; return function(Target,Origin){ F.prototype = Origi ...
- Android ORC文字识别之识别身份证号等(附源码)
项目地址https://github.com/979451341/OrcTest 我们说说实现这个项目已实现的功能,能够截图手机界面的某一块,将这个某一块图片的Bitmap传给tess-two的代码来 ...
- conda的使用(附带远程文件传输命令)
1 环境管理 1.1查看当前系统下的环境 conda info -e 创建新的环境 # 指定python版本为3.6,注意至少需要指定python版本或者要安装的包 conda create -n m ...
- 201671010142 2017-2 《java第十章学习感悟》
组件:Java把由Component类的子类或间接子类创建的对象称为一个组件. 容器:是Java中能容纳和排列组件的组件. Container类提供了一个方法add(),用来在容器类组件对象中添加其他 ...
- python简单爬虫 用lxml库解析数据
目标:爬取湖南大学2018年本科招生章程 url:http://admi.hnu.edu.cn/info/1026/2993.htm 页面部分图片: 使用工具: Python3.7 火狐浏览器 PyC ...
- easyui datagrid 后台分页,前端如何处理
module.exports = { queryMethod(){ let params = checkQueryParams.call(this); if (!params) { return; } ...
- Linux Network Command
查看 内外网访问ipnetstat -an download file from server scp -r root@139.xxx.xxx.82:~/virtualbox.box /tmp/
- myeclipse 与 mysql 的连接
在小学期的学习中,我了解了myeclipse的开发环境,与mysql连接,可对数据库进行增.删.改.查等操作,以下是在myeclipse中连接数据库的代码. package cn.neusoft.my ...
- Vue语法学习第五课——条件渲染
① v-if .v-else-if .v-else <div v-if="type === 'A'"> A </div> <div v-else-if ...