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 ...
随机推荐
- 在Python中定义和使用抽象类的方法
https://www.jb51.net/article/87710.htm 像java一样python也可以定义一个抽象类. 在讲抽象类之前,先说下抽象方法的实现. 抽象方法是基类中定义的方法,但却 ...
- Delphi如何处理在进行大量循环时,导致的应用程序没有响应的情况
一般用在比较费时的循环中,往往导致应用程序没有响应,此时在比较费时的程序体中加入Application.ProcessMessages即可解决,该语句的作用是检查并先处理消息队列中的其他消息. 例如, ...
- 再谈Promise
方法 构造函数 接受的参数是一个带两个Function参数的函数,实际的异步代码编写在这个函数里,成功后调用第一个参数,失败调用第二个: Promise.prototype.catch 当构造函数里调 ...
- jinfo
jinfo是jdk自带的命令,用来查看.修改jvm的配置参数. [weblogic@host bin]$ jinfo-bash: jinfo: command not found[weblogic@h ...
- python实现类似于Matlab中的magic函数
参考这篇文章的代码封装了一个类似Matlab中的magic函数,用来生成魔方矩阵. #!/usr/bin/env python # -*- coding: utf-8 -*- import numpy ...
- Golang学习教程
字节跳动已经全线从Python转Golang了,可能开始学习Golang这门语言会觉得无所适从,和Java,C++,Python等都不大一样,但是用多了会发现这门语言设计的还是很优雅的,下面总结Gol ...
- linux java 安装
对于java的开发,有openJDK 和 orcale jdk两种,大多数的linux 系统都会内置openjdk的安装包,但是大多数java项目的开发都是基于orcale jdk的,所以安装orca ...
- lua 中protobuf repeated 嵌套类 复合类型
PB基础知识科普 syntax = "proto2"; package PB; message Item { required string name = ; } message ...
- Oracle误删除数据的恢复方法(转)
来源:原创网站北京北亚数据恢复中心,转载须注明出处. 学习数据库时,我们只是以学习的态度,考虑如何使用数据库命令语句,并未想过工作中,如果误操作一下,都可能导致无可挽回的损失.当我在工作中真正遇到这些 ...
- 阅历>感悟
1.强扭的瓜不甜.在招聘的时候,面试官看不上你,你也不用赖着要去,你去能干好工作吗?面试官通常比你更清楚这个是事情.在比如谈恋爱,姑娘有更好的目标,不喜欢你了,决定离开你了,你再怎么挽留都是没意义的, ...