python课堂整理31----configparser模块,,一、功能:为配置文件
python课堂整理31----configparser模块,,一、功能:为配置文件
一、功能:为配置文件开发
创建一个配置文件:
import configparserconfig = configparser.ConfigParser()config["DEFAULT"] = { "ServerAliveInterval": ‘45‘, ‘Compression‘: ‘yes‘, ‘CompressionLevel‘:‘9‘}config[‘bitbuckket.org‘] = {} #创建一个空字典config[‘bitbuckket.org‘][‘User‘] = ‘jinling‘config[‘topsecret.server.com‘] = {} #创建一个空字典topsecret = config[‘topsecret.server.com‘]topsecret[‘Host Port‘] = ‘50022‘topsecret[‘ForwardX11‘] = ‘no‘config[‘DEFAULT‘][‘ForwardX11‘] = ‘yes‘ #向DEFAULT块添加一个新的键值对with open(‘example.ini‘, ‘w‘) as configfile: config.write(configfile)
-----------------------------增删改查-----------
#查
import configparserconfig = configparser.ConfigParser() #重要print(config.sections()) # 查看文件里的块(除DEFAULT) 这里因为没有读入文件,所以为空config.read(‘example.ini‘) #读入之前创建的配置文件print(config.sections()) # 查看文件里的块(除DEFAULT)print(‘bitbuckket.org‘ in config) #判断这个快是否在文件中print(config[‘bitbuckket.org‘][‘user‘]) #获取bitbuckket.org下user的值,这里user不区分大小写print(config[‘DEFAULT‘][‘compression‘]) #获取DEFAULT下compression的值
import configparserconfig = configparser.ConfigParser() #重要config.read(‘example.ini‘)#读入之前创建的配置文件for key in config[‘bitbuckket.org‘]: #会把DEFAULT下的键也循环打印出来 print(key)print(config.options(‘bitbuckket.org‘)) #获取键,并放在一个列表(同样把默认的键也放了进去)print(config.items(‘bitbuckket.org‘))#获取键和值,并放到一个列表print(config.get(‘bitbuckket.org‘, ‘compression‘)) #到bitbuckket.org快下获取compression的值
#增
import configparserconfig = configparser.ConfigParser() #重要config.read(‘example.ini‘)#读入之前创建的配置文件config.add_section(‘yuan‘) #添加一个块config.set(‘yuan‘, ‘k1‘,‘111‘) #向这个块添加一个键值对、config.write(open(‘jin.cfg‘, ‘w‘)) #把修改后的内容写入文件,可以同名覆盖
#删
import configparserconfig = configparser.ConfigParser() #重要config.read(‘example.ini‘)#读入之前创建的配置文件# config.add_section(‘yuan‘) #添加一个块# config.set(‘yuan‘, ‘k1‘,‘111‘) #向这个块添加一个键值对、config.remove_section(‘topsecret.server.com‘) #删除这个块,连同下面的所有键值对config.write(open(‘example.ini‘, ‘w‘)) #把修改后的内容写入文件,可以同名覆盖
import configparserconfig = configparser.ConfigParser() #重要config.read(‘example.ini‘)#读入之前创建的配置文件# config.add_section(‘yuan‘) #添加一个块# config.set(‘yuan‘, ‘k1‘,‘111‘) #向这个块添加一个键值对、# config.remove_section(‘topsecret.server.com‘) #删除这个块,连同下面的所有键值对config.remove_option(‘bitbuckket.org‘, ‘user‘) #删除某个键值对config.write(open(‘example.ini‘, ‘w‘)) #把修改后的内容写入文件,可以同名覆盖
python课堂整理31----configparser模块
相关内容
- python发送邮件,, 在实际开发中,有
- csp 2017年12月第二题 游戏 Python题解100分,,题意就不重复
- Python爬取全书网小说,免费看小说,,什么是网络爬虫网
- windows下安装python包,,1、windows下
- pymysql基本使用规则,,1、执行SQL#!/
- python学习之工资结算,,import os,
- Python3之harbor sdk api,,环境python3源
- pyorient,,简介pyorient
- centos7+nginx+python3+django+uwsgi配置,,学了一下python
- 【Python 09】汇率兑换2.0-2,,分支语句:根据判断条
评论关闭