day23_3_configparse
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# ------------------------------------------------------------
# 参考资料:
# python模块(转自Yuan先生) - 狂奔__蜗牛 - 博客园
# https://www.cnblogs.com/guojintao/articles/9070485.html
#
# 14.2. configparser — Configuration file parser — Python 3.7.0 documentation
# https://docs.python.org/3/library/configparser.html
# ------------------------------------------------------------
# ******************** day23_3_configparse *******************
# ******************** day23_3_configparse *******************
# =====>>>>>>内容概览
# =====>>>>>>内容概览 # ------------------------------------------------------------
# # 1、configparse简介
# # # ConfigParser模块在python3中修改为configparser.这个模块定义了一个ConfigParser类,该类
# # # 的作用是使用配置文件生效,配置文件的格式和windows的INI文件的格式相同
# # # 该模块的作用 就是使用模块中的RawConfigParser()、ConfigParser()、 SafeConfigParser()
# # # 这三个方法(三者择其一),创建一个对象使用对象的方法对指定的配置文件做增删改查 操作。
# ------------------------------------------------------------ # ------------------------------------------------------------
# # 2、configparse生成配置文件
# # # 生成的ini配置文件中,标签是的字符按照原来的输入生成,但是键名就是全部都会被转化成小写
# ------------------------------------------------------------ # ------------------------------------------------------------
# # 3、对 configparse生成配置文件 进行读取
# ------------------------------------------------------------ # ------------------------------------------------------------
# # 3.1、使用for对 configparse生成配置文件 进行读取
# # # 对自己所在的标签的键值读取完后,会默认读取[DEFAULT]的内容
# ------------------------------------------------------------ # ------------------------------------------------------------
# # 4、config.options、config.items、config.get
# # # 对 configparse生成配置文件 进行读取,以列表的形式输出、值
# # # 对自己所在的标签的键值读取完后,会默认读取[DEFAULT]的内容
# ------------------------------------------------------------ # ------------------------------------------------------------
# # 5、add_section、 config.write、config.set
# # #对配置文件写入标签,键名 + 键值
# ------------------------------------------------------------ # ------------------------------------------------------------
# # 6、remove_section
# # # 删除标签以及对应的内容
# ------------------------------------------------------------ # ------------------------------------------------------------
# # 7、remove_option
# # # # 删除标签内的'forwardx11'键名与对应的键值
# ------------------------------------------------------------
# ------------------------------------------------分割线-------------------------------------------------
# ------------------------------------------------分割线-------------------------------------------------
# ------------------------------------------------分割线-------------------------------------------------
'''
# ------------------------------------------------------------
# # 1、configparse简介
# # # ConfigParser模块在python3中修改为configparser.这个模块定义了一个ConfigParser类,该类
# # # 的作用是使用配置文件生效,配置文件的格式和windows的INI文件的格式相同
# # # 该模块的作用 就是使用模块中的RawConfigParser()、ConfigParser()、 SafeConfigParser()
# # # 这三个方法(三者择其一),创建一个对象使用对象的方法对指定的配置文件做增删改查 操作。 example.ini文件内容:
[DEFAULT] ---->>标签,默认是[]包围起来的
serveraliveinterval = 45 ---->>serveraliveinterval键名, 45 键值
# # #
# ------------------------------------------------------------
''' '''
# ------------------------------------------------------------
# # 2、configparse生成配置文件
# # # 生成的ini配置文件中,标签是的字符按照原来的输入生成,但是键名就是全部都会被转化成小写
# ------------------------------------------------------------
'''
#
# import configparser
# config = configparser.ConfigParser()
# config['DEFAULT'] = {'ServerAliveInterval': '45',
# 'Compression': 'yes',
# 'CompressionLevel':'9'
# }
#
# config['Bitbucket.org'] = {}
# config['Bitbucket.org']['User'] = 'hg'
#
# config['topsecret.server.com'] = {}
# topsecret = config['topsecret.server.com']
# topsecret['Port'] = '50022' # mutates the parser
# topsecret['ForwardX11'] = 'no' # same here
#
# config['DEFAULT']['ForwardX11'] = 'yes'
# with open('example.ini','w') as configfile:
# config.write(configfile)
#
# # D:\Anaconda3\python.exe D:/C_cache/py/day23-re_logging_hashlib_MoKuaiDaoRu/day23_3_configparse.py
# #
# # Process finished with exit code 0 '''
# ------------------------------------------------------------
# # 3、对 configparse生成配置文件 进行读取 'example.ini'文件内容:
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes [Bitbucket.org]
user = hg [topsecret.server.com]
port = 50022
forwardx11 = no
# ------------------------------------------------------------
''' # import configparser
# config = configparser.ConfigParser()
# # [] ,没有指定读取的文件
# print("config.sections():\t".expandtabs(40), config.sections())
#
# config.read('example.ini')
# # 读标签,除默认标签外
# print("read,config.sections():\t".expandtabs(40), config.sections())
#
# print("'bytebong.com' in config:\t".expandtabs(40), 'bytebong.com' in config)
#
# print("config['Bitbucket.org']:\t".expandtabs(40), config['Bitbucket.org'])
# print("config['Bitbucket.org']['User']:\t".expandtabs(40), config['Bitbucket.org']['User'])
#
# print("config['config['DEFAULT']['Compressiong']:\t".expandtabs(40), \
# config['DEFAULT']['Compression'])
# print("config['Bitbucket.org']['User']:\t".expandtabs(40), \
# config['topsecret.server.com']['ForwardX11'])
#
#
# # D:\Anaconda3\python.exe D:/C_cache/py/day23-re_logging_hashlib_MoKuaiDaoRu/day23_3_configparse.py
# # config.sections(): []
# # read,config.sections(): ['Bitbucket.org', 'topsecret.server.com']
# # 'bytebong.com' in config: False
# # config['Bitbucket.org']: <Section: Bitbucket.org>
# # config['Bitbucket.org']['User']: hg
# # config['config['DEFAULT']['Compressiong']: yes
# # config['Bitbucket.org']['User']: no
# #
# # Process finished with exit code 0 '''
# ------------------------------------------------------------
# # 3.1、使用for对 configparse生成配置文件 进行读取
# # # 对自己所在的标签的键值读取完后,会默认读取[DEFAULT]的内容 'example.ini'文件内容:
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes [Bitbucket.org]
user = hg [topsecret.server.com]
port = 50022
forwardx11 = no
# ------------------------------------------------------------
'''
#
# import configparser
# config = configparser.ConfigParser()
# config.read('example.ini')
#
# for key in config['Bitbucket.org']:
# print(key)
# # D:\Anaconda3\python.exe D:/C_cache/py/day23-re_logging_hashlib_MoKuaiDaoRu/day23_3_configparse.py
# user
# serveraliveinterval
# compression
# compressionlevel
# forwardx11
#
# Process finished with exit code 0 '''
# ------------------------------------------------------------
# # 4、config.options、config.items、config.get
# # # 对 configparse生成配置文件 进行读取,以列表的形式输出、值
# # # 对自己所在的标签的键值读取完后,会默认读取[DEFAULT]的内容 'example.ini'文件内容:
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes [Bitbucket.org]
user = hg [topsecret.server.com]
port = 50022
forwardx11 = no
# ------------------------------------------------------------
'''
#
# import configparser
# config = configparser.ConfigParser()
# config.read('example.ini')
# # 输出键名
# print("config.options('Bitbucket.org'): \n",config.options('Bitbucket.org'))
# # 输出键名+键值
# print("config.items('Bitbucket.org'):\n",config.items('Bitbucket.org'))
# # 对输出键名+键值,进行值获取
# print("config.get('Bitbucket.org'):\n",config.get('Bitbucket.org','serveraliveinterval'))
#
#
# # D:\Anaconda3\python.exe D:/C_cache/py/day23-re_logging_hashlib_MoKuaiDaoRu/day23_3_configparse.py
# # ['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11']
# #
# # Process finished with exit code 0
# '''
# ------------------------------------------------------------
# # 5、add_section、 config.write、config.set
# # #对配置文件写入标签,键名 + 键值 'example.ini'文件内容:
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes [Bitbucket.org]
user = hg [topsecret.server.com]
port = 50022
forwardx11 = no
# ------------------------------------------------------------
'''
#
# import configparser
#
# config = configparser.ConfigParser()
# config.read('example.ini')
# # 添加标签
# config.add_section("YUAN")
#
# # 添加标签
# config.add_section("AAa")
# # 对标签的内容进行写入 键名 + 键值
# config.set('AAa','k9','99999')
#
# # 写入
# with open('example.ini','w') as configfile:
# config.write(configfile)
#
# D:\Anaconda3\python.exe D:/C_cache/py/day23-re_logging_hashlib_MoKuaiDaoRu/day23_3_configparse.py
#
# Process finished with exit code 0 '''
# ------------------------------------------------------------
# # 6、remove_section
# # # 删除标签以及对应的内容 'example.ini'文件内容:
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes [bitbucket.org]
user = hg [YUAN] [AAa]
k9 = 99999
# ------------------------------------------------------------
'''
#
# import configparser
#
# config = configparser.ConfigParser()
# config.read('example.ini')
#
# # 删除标签以及对应的内容
# config.remove_section('topsecret.server.com')
#
# # 写入
# with open('example.ini','w') as configfile:
# config.write(configfile)
#
# # D:\Anaconda3\python.exe D:/C_cache/py/day23-re_logging_hashlib_MoKuaiDaoRu/day23_3_configparse.py
# #
# # Process finished with exit code 0
#
# '''
# ------------------------------------------------------------
# # 7、remove_option
# # # # 删除标签内的'forwardx11'键名与对应的键值 'example.ini'文件内容:
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes [bitbucket.org]
user = hg [YUAN] [AAa]
k9 = 99999
# ------------------------------------------------------------
'''
#
# import configparser
#
# config = configparser.ConfigParser()
# config.read('example.ini')
#
# # 删除标签内的'forwardx11'键名与对应的键值
# config.remove_option('topsecret.server.com','forwardx11')
# # 写入
# with open('example.ini','w') as configfile:
# config.write(configfile) # D:\Anaconda3\python.exe D:/C_cache/py/day23-re_logging_hashlib_MoKuaiDaoRu/day23_3_configparse.py
#
# Process finished with exit code 0
day23_3_configparse的更多相关文章
随机推荐
- 25. object类中的一些方法分析
1. Object java是面向对象语言,所以其核心思想: 找合适的对象,做合适的事 Object是所有类的终极基类.任何一个类都继承了Object类 2. Object的部分函数列表 1)Stri ...
- HTTP状态码及请求头
状态码 状态码告知从服务器端返回的请求结果 一般可分为5个大类 1XX Informational(信息性状态码) 2XX Success(成功状态码) 3XX Redirection(重定向状态码) ...
- python 打印出水仙花数
打印出三位水仙花数方法及解释 num = 100while num <= 999: #这里num 小于等于999 则运行 填1000也可以 a = num % 10 #num对10取余 b = ...
- mongodb常用基本命令
一.数据库常用命令 1.help查看命令提示 help db.help() db.test.help() db.test.find().help() 2.创建.切换数据库 use movies 3.查 ...
- 视频质量评测标准——VMAF
阿里云视频云直播转码每天都会处理大量的不同场景.不同编码格式的直播流.为了保证高画质,团队借助VMAF标准来对每路转码的效果做质量评估,然后进行反馈.调优.迭代.这么做的原因在于,像动作片.纪录片.动 ...
- 双目立体匹配经典算法之Semi-Global Matching(SGM)概述:代价聚合(Cost Aggregation)
由于代价计算步骤只考虑了局部的相关性,对噪声非常敏感,无法直接用来计算最优视差,所以SGM算法通过代价聚合步骤,使聚合后的代价值能够更准确的反应像素之间的相关性,如图1所示.聚合后的新的代价值保存 ...
- scanf 与getchar区别
#include<stdio.h> void main() { int c; c=getchar(); //scanf("%c",&c); if(c!=' ...
- HDU—4046 Panda (线段树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4046 题意:给出一个字符串,统计这个字符串任意区间中"wbw"出现的次数. 规定两 ...
- Jmeter----参数化之csv文件
用户编码:一般不用填写,如果有中文的时候,需要填写,填写utf-8就可以 变量名词:是指的把数据保存在什么变量里面,其他的默认就好了 Http请求和用户参数设置的一样
- 007-Java可变个数形参重载【数组和...】
重载方法时,可变个数形参的方法有两种方式 数组重载 ...重载 对两种方法,其实是一致的,示例如下: public class MethodArgsTest { //可变个数形参的格式:数据类型... ...