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的更多相关文章
随机推荐
- 笔记46 Hibernate快速入门(三)
Hibernate相关概念 一.事物概念 Hibernate的任何对数据有改动的操作,都应该被放在事务里面. hibernate中的事务由s.beginTransaction();开始由s.getTr ...
- html 通过input video canvas 打开摄像头 定制相机
在机缘巧合之下,了解到用HTML5和javascript调用摄像头来实现拍照功能,今天就把大致原理写下来.页面布局很简单,就是一个input标签,两个HTML5元素video.canvas和一个but ...
- IconFont 图标的3种引用方式
第一步:进入阿里巴巴矢量图网站:http://www.iconfont.cn/ 阿里巴巴矢量图 第二步:搜索你分类的关键字---然后加入购物车,下载到本地,然后解压,会将合并后的字体文件及自动生成 ...
- leetcood学习笔记-102-二叉树的层次遍历
题目描述: 方法一; class Solution(object): def levelOrder(self, root): """ :type root: TreeNo ...
- 后缀自动机求字典序第k小的串——p3975
又领悟到了一点新的东西,后缀自动机其实可以分为两个数据结构,一个是后缀树,还有一个是自动机 后缀树用来划分endpos集合,并且维护后缀之间的关系,此时每个结点代表的是一些后缀相同且长度连续的子串 自 ...
- NX二次开发-重命名装配组件
在GC工具里面是有一个重命名装配组件的命令的,除了这个外,好像没看到NX里还有其他可以重命名装配组件的命令,本来以为在UFUN ASSEM装配的头文件里会有更改装配部件名字的函数,但是没有找到,可能没 ...
- NX二次开发-获得制图中对象的坐标点UF_DRF_ask_origin
#include <uf.h> #include <uf_ui.h> #include <uf_drf.h> #include <uf_obj.h> # ...
- NX二次开发-比较两个string是否相等
NX11+VS2013 #include <uf.h> #include <uf_ui.h> UF_initialize(); string A = "ABC&quo ...
- 全局唯一标识符(GUID,Globally Unique Identifier)
全局唯一标识符(GUID,Globally Unique Identifier)是一种由算法生成的二进制长度为128位的数字标识符.GUID主要用于在拥有多个节点.多台计算机的网络或系统中.在理想情况 ...
- (转)HashSet<T>类
转载于:http://www.importnew.com/6931.html HashSet<T>类主要是设计用来做高性能集运算的,例如对两个集合求交集.并集.差集等.集合中包含一组不重复 ...