Python3学习之路~5.11 configparser模块
用于生成和修改常见配置文档,当前模块的名称在 python 2.x 版本中为 ConfigParser, python 3.x 版本中变更为 configparser。
来看一个好多软件的常见文档格式如下
- [DEFAULT]
- ServerAliveInterval = 45
- Compression = yes
- CompressionLevel = 9
- ForwardX11 = yes
- [bitbucket.org]
- User = hg
- [topsecret.server.com]
- Port = 50022
- ForwardX11 = no
如果想用python生成一个这样的文档怎么做呢?
- import configparser
- config = configparser.ConfigParser()
- config["DEFAULT"] = {'ServerAliveInterval': '',
- 'Compression': 'yes',
- 'CompressionLevel': ''}
- config['bitbucket.org'] = {}
- config['bitbucket.org']['User'] = 'hg'
- config['topsecret.server.com'] = {}
- topsecret = config['topsecret.server.com']
- topsecret['Host Port'] = '' # mutates the parser
- topsecret['ForwardX11'] = 'no' # same here
- config['DEFAULT']['ForwardX11'] = 'yes'
- with open('example.ini', 'w') as configfile:
- config.write(configfile)
写完了还可以再读出来
- import configparser
- config = configparser.ConfigParser()
- print(config.sections()) # []
- config.read('example.ini')
- print(config.sections()) # ['bitbucket.org', 'topsecret.server.com']
- print(config.default_section) # DEFAULT
- print('bitbucket.org' in config) # True
- print('bytebong.com' in config) # False
- print(config.has_section('DEFAULT')) # False
- print(config.has_section('topsecret.server.com')) # True
- print(config['bitbucket.org']['User']) # hg
- print(config.get('bitbucket.org','User')) # hg
- print(config['DEFAULT']['Compression']) # yes
- topsecret = config['topsecret.server.com']
- print(topsecret['ForwardX11']) # no
- print(topsecret['host port']) #
- print(config.get('topsecret.server.com','host port')) #
- print(config.getint('topsecret.server.com','host port')) #
- for key in config['bitbucket.org']:
- print(key)
- # 输出:
- # user
- # serveraliveinterval
- # compression
- # compressionlevel
- # forwardx11
- print(config.options('bitbucket.org'))
- # 输出:['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11']
- print(config.items('bitbucket.org'))
- # 输出:[('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'hg')]
- print(config['bitbucket.org']['ForwardX11']) # yes
- print(config.defaults())
- # 输出:OrderedDict([('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes')])
configparser-读
configparser增删改语法
- import configparser
- config = configparser.ConfigParser()
- config.read('example.ini')
- # # 删
- # config.remove_section('bitbucket.org')
- # config.remove_option('topsecret.server.com','forwardx11')
- # config.write(open('example2.cfg','w'))
- # # 增
- # print(config.has_section('newSection')) # False
- # config.add_section('newSection')
- # config.write(open('example3.cfg', "w"))
- # 改
- config.set('bitbucket.org','User','Bob')
- config.write(open('example4.cfg','w'))
configparser-增删改
Python3学习之路~5.11 configparser模块的更多相关文章
- Python3学习之路~9.1 paramiko模块:实现ssh执行命令以及传输文件
我们一般使用linux的时候,都是在Windows上安装一个ssh客户端连接上去.那么从一台linux如何连接到另一条linux呢?使用ssh命令即可,因为每台linux机器自己都有一个ssh客户端. ...
- Python3学习之路~5.5 sys模块
用于提供对解释器相关的操作 sys.argv 命令行参数List,第一个元素是程序本身路径 sys.exit(n) 退出程序,正常退出时exit(0) sys.version 获取Python解释程序 ...
- Python3学习之路~5.3 random模块
random模块常用方法: import random # 随机数 print(random.random()) # 生成一个0到1的随机浮点数,0 <= n < 1.0 print(ra ...
- Python3学习之路~5.13 re模块 正则表达式
re模块用于对python的正则表达式的操作. 常用正则表达式符号 字符数字: . 匹配除换行符以外的任意字符,即[^\n] \s 匹配任意空白符(如\t.\n.\r ) \S 匹配任意非空白符 \w ...
- Python3学习之路~5.10 PyYAML模块
Python也可以很容易的处理ymal文档格式,只不过需要安装一个模块,参考文档:http://pyyaml.org/wiki/PyYAMLDocumentation
- Python3学习之路~5.8 shelve模块
shelve模块是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式 import shelve import datetime name = [& ...
- Python3学习之路~5.4 os模块
用于提供系统级别的操作 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相当于shel ...
- Python3学习之路~0 目录
目录 Python3学习之路~2.1 列表.元组操作 Python3学习之路~2.2 简单的购物车程序 Python3学习之路~2.3 字符串操作 Python3学习之路~2.4 字典操作 Pytho ...
- Python3学习之路
python基础知识点 1.python基础知识点汇总 2.python常用数据类型 3.python之列表 4.python之字符串 5.python常用数据运算符 6.python之字典 7.py ...
随机推荐
- node出现 Error: listen EACCES 0.0.0.0:8080错误
Error: listen EACCES 0.0.0.0:8080 at Object._errnoException (util.js:1022:11) at _exceptionWithHostP ...
- vue移动端flexible.js结合Muse-ui使用和vux的小坑
因为公司有个项目有webapp的需求,在前期准备的期间考虑过使用ionic,毕竟该项目web端的框架使用的是Angular,项目组的人也都比较熟悉,但是我们毕竟只是做个移动的网页,不想用ionic那么 ...
- 删除maven仓库中的LastUpdated文件
转自:http://www.oschina.net/code/snippet_151849_49131 @echo off rem create by sunhao(sunhao.java@gmail ...
- 【12c】12c RMAN新特性之通过网络远程恢复数据库(RESTORE/Recover from Service)
[12c]12c RMAN新特性之通过网络远程恢复数据库(RESTORE/Recover from Service) 通过网络远程恢复数据库(Restore/Recover from Service) ...
- 4 playlook-Jinja2 filter
jinja2模板,不多解释,请看例子 [root@LeoDevops jinjatwo]# cat j1.yaml - hosts: u12 gather_facts: False vars: lis ...
- sip协议中文讲解
https://blog.csdn.net/qiuchangyong/article/details/50748854
- 获取请求IP
服务器获取客户端或者网页的请求,获取IP时需要注意,并不是直接 request.getRemoteAddr(); 就可以了,因为一个请求到达服务器之前,一般都会经过一层或者多层代理服务器,比如反向代理 ...
- 七、Sql Server 基础培训《进度7-笛卡尔积(知识点+实际操作)》
知识点: 1.笛卡尔介绍 笛卡尔,近代法国著名哲学家.物理学家.数学家.神学家. 主要成就概述 笛卡尔在科学上的贡献是多方面的.笛卡尔不仅在哲学领域里开辟了一条新的道路,同时笛卡尔又是一勇于探索的科学 ...
- mysql按位的索引判断值是否为1
DELIMITER $$ DROP FUNCTION IF EXISTS `value_of_bit_index_is_true`$$/*计算某个数字的某些索引的位的值是否都为1,索引类似1,2,3, ...
- iOS - Harpy版本更新工具兼容版本第三方库
Harpy(兼容版) git地址:https://github.com/yangchao0033/Harpy ###(iOS5-9适配版本,基于ArtSabintsev/Harpy v3.4.5) 提 ...