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,这里简单的做 ...
随机推荐
- 浅谈UI自动化测试
最近一直在学习python,正好部门技术结构调整,就开始了点工向UI自动化测试的转变,我要说瞌睡来了就掉枕头么? 不过还好,可以将python的学习成果在自动化测试中实践... 1.about自动化测 ...
- Spring @Scheduled定时任务的fixedRate,fixedDelay,cron执行差异
import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import org.sp ...
- Android 让图片等比例缩放的三种方法
方法一:客户端等比例 前提条件:服务器端需要返回原始图片的“宽和高”或者“宽高缩放比例”,客户端要显示的图片的宽或者高只要其一是固定的(例如:高度为200,宽度未知,或者高度为400宽度未知) 在这种 ...
- android java.lang.NoClassDefFoundError: cn.yw.lib.viewpagerfragment.ViewPagerFragmentActivity
假如你判断你的项目没有异常,并且该注册的Activity也注册了.那么解决这个问题的方法就是:右击项目->properties->Java Build Path->Order and ...
- TerraGate软件安装后,不能启动的解决办法
在服务端安装Skyline的TerraGate软件的时候,大家可能会遇到过这样的问题,“TerraGate软件安装后,不能启动”,很多时候,这个问题是因为TerraGate设 置的端口号已经被占用造成 ...
- 在Oracle中执行动态SQL的几种方法
转载:在Oracle中执行动态SQL的几种方法 以下为内容留存: 在Oracle中执行动态SQL的几种方法 在一般的sql操作中,sql语句基本上都是固定的,如:SELECT t.empno,t.en ...
- CF809E Surprise me! 莫比乌斯反演、虚树
传送门 简化题意:给出一棵\(n\)个点的树,编号为\(1\)到\(n\),第\(i\)个点的点权为\(a_i\),保证序列\(a_i\)是一个\(1\)到\(n\)的排列,求 \[ \frac{1} ...
- Luogu4609 FJOI2016 建筑师 第一类斯特林数
题目传送门 题意:给出$N$个高度从$1$到$N$的建筑,问有多少种从左往右摆放这些建筑的方法,使得从左往右看能看到$A$个建筑,从右往左看能看到$B$个建筑.$N \leq 5 \times 10^ ...
- SpringBoot favicon.ico
默认的favicon.ico spring boot 默认输出spring的logo, 可以使用spring.mvc.favicon.enabled=false将其关闭 ############### ...
- prop和attr的比较
prop来获取或设置固有属性 removeProp() 删除固有属性 attr来获取或设置自定义属性 removeAttr() 删除自定义属性 案例:全选与全不选 <body> ...