python 模块之-configparser
python 模块configparser 配置文件模块
import configparser
config = configparser.ConfigParser()config["DEFAULT"] = {'ServerAliveInterval': '45', 'Compression': 'yes', 'CompressionLevel': '9'} config['bitbucket.org'] = {'User': 'hg'}config['topsecret.server.com'] = {'Host Port':'50022', 'ForwardX11','no'}with open('example.ini', 'w') as configfile: #把键值对写入 配置文件example.ini config.write(configfile)[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes [bitbucket.org]
User = hg [topsecret.server.com]
Port = 50022
ForwardX11 = no
config.read('example.ini') # 关联文件才能操作里面键值
print(config.sections()) #['bitbucket.org', 'topsecret.server.com'] 查看段
print('bytebong.com' in config)# False 判断
print(config['bitbucket.org']['User']) # hg 查看值
print(config['DEFAULT']['Compression']) #yes 查看值
print(config['topsecret.server.com']['ForwardX11']) #no 查看值
for key in config['bitbucket.org']: #循环打印[bitbucker.org]的键
print(key)
###打印段[bitbucker.org]的所有键
print(config.options('bitbucket.org'))#['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11']
###循环打印段[bitbucker.org]的所有键值
print(config.items('bitbucket.org')) #[('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'hg')]
###打印段[bitbucket.org] 键compression 的值
print(config.get('bitbucket.org','compression'))#yes
config.add_section('yuan') #增加一个段 [yuan]
config.remove_section('topsecret.server.com') # 删除一个段[topsecret.server.com]
config.remove_option('bitbucket.org','user') # 删除这个[bitbucket.org]段里面的键和值User = hg
config.set('bitbucket.org','k1','11111') # 添加[bitbucket.org]段里面 键值 k1 = 11111
config.write(open('i.cfg', "w") # 写入的话 必须先执行这条命令
python 模块之-configparser的更多相关文章
- Python模块之: ConfigParser 配置文件读取
Python模块之: ConfigParser 配置文件读取 ConfigParser用于读写类似INI文件的配置文件,配置文件的内容可组织为组,还支持多个选项值(option-value)类型. ...
- Python模块学习 - ConfigParser
配置文件 很多软件都用到了配置文件,像git运行的时候会读取~/gitconfig,MySQL运行的时候会读取/etc/my.cnf,Python 提供的包管理工具pip命令,也会去读取~/.pip/ ...
- Python模块之ConfigParser - 读写配置文件
Python 标准库的 ConfigParser 模块提供一套 API 来读取和操作配置文件. 配置文件的格式 a) 配置文件中包含一个或多个 section, 每个 section 有自己的 opt ...
- Python 模块续 configparser、shutil、XML、paramiko、系统命令、
一.configparse # 注释1 ; 注释2 [section1] # 节点 k1 = v1 # 值 k2:v2 # 值 [section2] # 节点 k1 = v1 # 值 1.获取所有节点 ...
- Python模块:configparser、hashlib、(subprocess)
configparser模块: 此模块用于生成和修改常见配置文档. 一个常见配置文件(.ini的后缀名)格式如下: [DEFAULT] # DEFAULT 是指后面的字典里都会默认有的内容 Serve ...
- python模块之configparser
configparser用于处理特定格式的文件,其本质上是利用open来操作文件. # 注释1 ; 注释2 [section1] # 节点 k1 = v1 # 值 k2:v2 # 值 [section ...
- python模块:configparser
"""Configuration file parser. A configuration file consists of sections, lead by a &q ...
- python模块之ConfigParser: 用python解析配置文件
在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是ConfigParser,这里简单的做一些介 ...
- Python 模块之 ConfigParser: 用 Python 解析配置文件
在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在 Python 里更是如此,在官方发布的库中就包含有做这件事情的库,那就是 ConfigParser,这里简单的做 ...
随机推荐
- linux问题总结
编写后台进程的管理脚本,使用service deamon-name stop的时候,出现如下提示:/sbin/service: line 66: 23299 Terminated env -i LAN ...
- 查看电脑本机的ip地址
第一步:打开cmd 第二步:输入:ipconfig,回车 第三步:找到无线局域网适配器下的IPv4地址,那就是本机的ip地址
- 9、链表 & 状态机 & 多线程
链表的引入 从数组的缺陷说起 数组有2个缺陷:一个是数组中所有元素的类型必须一致:第二个是数组的元素个数必须事先制定并且一旦指定之后不能更改. 如何解决数组的2个缺陷:数组的第一个缺陷靠结构体去解决. ...
- java算法----排序----(3)冒泡排序
package log; public class Test4 { /** * java算法---冒泡排序 * * @param args */ public static void main(Str ...
- mysql的聚簇索引与非聚簇索引的简短总结
[原文]https://www.jianshu.com/p/72763d47aa1a 在mysql数据库中,myisam引擎和innodb引擎使用的索引类型不同,myisam对应的是非聚簇索引,而in ...
- 【译】高级指南-深入JSX
title: 高级指南-深入JSX date: 2017-4-5 17:13:09 --- 深入JSX 从根本上来讲,JSX 仅仅是提供 React.createElement(component, ...
- 记一次yarn导致cpu飙高的异常排查经历
yarn就先不介绍了,这次排坑经历还是有收获的,从日志到堆栈信息再到源码,很有意思,下面听我说 问题描述: 集群一台NodeManager的cpu负载飙高. 进程还在但是看日志已经不再向Resourc ...
- Qt5.9使用QWebEngineView加载网页速度非常慢,问题解决
折腾了大半天终于解决了 原帖地址:https://bugreports.qt.io/browse/QTBUG-44763 BUG单下的留言讲明了问题发生的原因,那就是系统默认设置为自动寻找代理,而使用 ...
- [译]通往 Java 函数式编程的捷径
原文地址:An easier path to functional programming in Java 原文作者:Venkat Subramaniam 译文出自:掘金翻译计划 以声明式的思想在你的 ...
- linux第三次实践:ELF文件格式分析
linux第三次实践:ELF文件格式分析 标签(空格分隔): 20135328陈都 一.概述 1.ELF全称Executable and Linkable Format,可执行连接格式,ELF格式的文 ...