10、configparser模块
```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']
'50022'
>>> for key in config['bitbucket.org']: print(key) # for循环 bitbucket.org 字典的key
...
user
compressionlevel
serveraliveinterval
compression
forwardx11
>>> config['bitbucket.org']['ForwardX11']
'yes'
```
```python
[group1] # 支持的两种分隔符“=”, “:”
k1 = v1
k2:v2[group2]
k1 = v1import ConfigParserconfig = ConfigParser.ConfigParser()
config.read('i.cfg')# ########## 读 ##########
#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')# ########## 改写 ##########
#sec = config.remove_section('group1') # 删除section 并返回状态(true, false)
#config.write(open('i.cfg', "w")) # 对应的删除操作要写入文件才会生效#sec = config.has_section('wupeiqi')
#sec = config.add_section('wupeiqi')
#config.write(open('i.cfg', "w")) ##config.set('group2','k1',11111)
#config.write(open('i.cfg', "w"))#config.remove_option('group2','age')
#config.write(open('i.cfg', "w"))
```
转载于:https://www.cnblogs.com/vettel2018/p/10076219.html
10、configparser模块相关推荐
- python小白-day6 ConfigParser模块
ConfigParser模块 用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 来看一个好多软件的常见文档格式如下 用python生成上文档 ...
- Python基础(14)_python模块之configparser模块、suprocess
9.configparser模块 模块适用于配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键=值). 常见的软件格式文档格式如下: 1 ...
- configParser模块详谈
前言 使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是configParser configPars ...
- 小白的Python之路 day5 configparser模块的特点和用法
configparser模块的特点和用法 一.概述 主要用于生成和修改常见配置文件,当前模块的名称在 python 3.x 版本中变更为 configparser.在python2.x版本中为Conf ...
- python:实例化configparser模块读写配置文件
之前的博客介绍过利用python的configparser模块读写配置文件的基础用法,这篇博客,介绍下如何实例化,方便作为公共类调用. 实例化的好处有很多,既方便调用,又降低了脚本的维护成本,而且提高 ...
- Python 常用函数 configparser模块
使用ConfigParser模块读写ini文件 ConfigParserPython的ConfigParser Module中定义了3个类对INI文件进行操作.分别是RawConfigParser.C ...
- python中configparser详解_Python中的ConfigParser模块使用详解
1.基本的读取配置文件 -read(filename) 直接读取ini文件内容 -sections() 得到所有的section,并以列表的形式返回 -options(section) 得到该sect ...
- Python Configparser模块读取、写入配置文件
写代码中需要用到读取配置,最近在写python,记录一下. 如下,假设有这样的配置. [db] db_host=127.0.0.1 db_port=3306 db_user=root db_pass= ...
- python返回上一条指令必须包括_python基础(10)--模块
模块(module) 模块,用一坨代码实现了某个功能的代码集合 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来 ...
- Python之configparser模块详解和使用
1 configparser安装 pip3 install configparser 2 configparser简介 用来读取配置文件的python包: 一般做自动化测试的时候,会使用到这个模块,用 ...
最新文章
- 【pandas学习笔记】Series
- android 上传pdf文件,Android 加载PDF文件
- 13.while循环
- struts升级:FileUploadInterceptor在struts 2.3.14.2的jar中修改了方法acceptFile中的参数
- Spring Boot flyway的启动时机比较早
- Vue报错Cannot find module ‘webpack-cli/bin/config-yargs‘
- NSString 和 NSMutableString
- LeetCode 94. Binary Tree Inorder Traversal
- java流家族_1.1.2 完整的流家族
- Java 中的枚举 (enum)
- 如何写出一份好的解决方案
- 笔记本怎样连接无线网络
- sat数学可以用计算机吗,SAT考试数学是否可以使用计算器
- Word如何交叉引用以及交叉引用参考文献后使用逗号分隔
- linux内核中内存耗尽OOM killer
- 利用计算机解决古代数学问题鸡兔同笼,古代鸡兔同笼数学题:利用现代数学思维来解决...
- 晚上看了一个知乎评论区,我失眠了
- 小微企业适合用什么样的进销存软件?
- java修改图章图片_Java处理PDF图章的方法示例(代码)
- Fiido Riding苹果app的技术支持网址
热门文章
- HTMLjavaSkcriptCSSjQueryajax(六)
- [20171206]位图区一定在数据文件开头吗.txt
- 首个 Ubuntu 平板将于今年秋天面世
- 利用栈将html源码解析为节点树
- 第一节 42字符串基础
- jsp乱码介绍(续)
- NUMA与英特尔下一代Xeon处理器学习心得(10)
- java程序如何执行jar包_java程序如何打包成可执行jar包?
- java中process方法用处_Java Process 简略使用方法以及坑点
- python目录操作_Python 简明教程 --- 25,Python 目录操作