1、configparser的作用

mysql等很多文件的配置如下:

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes

[bitbucket.org]
User = hg

[topsecret.server.com]
Port = 50022
ForwardX11 = no

如何用python生成和更改类似的配置文件,需要使用configparser模块,是python3的内置模块,可以直接导入

2、如何写一个配置文件

方法:add_section(section) 添加一个新的section

import configparser
config = configparser.ConfigParser()
config = configparser.ConfigParser() #生成一个对象
print(type(config)) #默认会有DEFAULT节点
config["DEFAULT"] = {
'ServerAliveInterval': 45,
'Compression': 'yes',
'CompressionLevel': '9'
} #感觉有点像字典的配置,哈哈,给默认的DEFAULT #新增节点
config.add_section('hello.org')
config.set('hello.org','IP','192.168.9.12') config['bitbucket.org'] ={} #必须得先把节点加上,再在下面进行一个参数配置,否则会报错
config['bitbucket.org']['User'] = 'hg' #增加一个配置 #换种写法
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com'] #先把一个节点赋给一个变量也是OK的
topsecret["Host Port"] = '50022' config['DEFAULT']['ForwardX11'] = 'yes' #默认节点也可以这样搞 #将写好的配置文件写入文件
with open('example.ini','w') as configfile:
config.write(configfile)

3、读取一个配置文件

RawCnfigParser是最基础的INI文件读取类,ConfigParser、SafeConfigParser支持对%(value)s变量的解析。
read(filename) 直接读取ini文件内容
sections() 得到所有的section,并以列表的形式返回
options(section) 得到该section的所有option
items(section) 得到该section的所有键值对
get(section,option) 得到section中option的值,返回为string类型
getint(section,option) 得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat() 函数。

# [DEFAULT]
# compressionlevel = 9
# compression = yes
# serveraliveinterval = 45
# forwardx11 = yes
#
# [hello.org]
# ip = 192.168.9.12
# serveraliveinterval = 46
#
# [bitbucket.org]
# serveraliveinterval = 46
#
# [portal]
# url = http://%(host)s:%(port)s/Portal
# host = localhost
# port = 8888
#
# [topsecret.server.com]
# host port = 50022 import configparser
config = configparser.ConfigParser() #生成一个对象
config.read("example.ini") #这样就把配置文件读过来了 print(config.sections()) #打印一下节点,发现木有DEFAULT:['bitbucket.org', 'topsecret.server.com'] #打印某个节点下的键,会把DEFALUT的也打印一下,除非这个节点下的键将DEFAULT的值覆盖掉
print(config.options('bitbucket.org')) #将某个节点的配置以键值对的形式打印出来,是一个list
print(type(config.items('bitbucket.org')) ) #获取某个单个键的值
print(config.get('bitbucket.org','user'))
print(config.get('bitbucket.org','compression')) #也可以获取默认的,如果没有覆盖的话 print("key:",config.items('bitbucket.org')[0][0],"value:",config.items('bitbucket.org')[0][1])
print(config.get('portal','url')) #明显支持%(value)s的解析,挺好用的

4、配置文件的删和改

import configparser
config = configparser.ConfigParser() #生成一个对象 config.read('example.ini') config.remove_section('section1')
config.remove_option('bitbucket.org','user') config.set('portal','port','8888') config.write(open('example.ini', "w"))

 

configparser配置文件模块的更多相关文章

  1. configparser 配置文件模块

    #_author:star#date:2019/11/7# configparser 配置文件模块import configparserconfig=configparser.ConfigParser ...

  2. hashlib摘要算法模块,logging日志,configparser配置文件模块

    一.hashlib模块(摘要算法模块) 1.算法介绍 Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 什么是摘要算法呢? 摘要算法又称哈希算法.散列算法.它通过一个函数,把 ...

  3. configparser (配置文件) 模块

    主要内容来自景女神博客 内涵:该模块适用于配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键=值). 常见文档格式: [DEFAULT] ...

  4. day31 configparser 配置文件模块

    #__author__: Administrator #__date__: 2018/8/8 # configparse 生成配置文件,配置文会以件.ini结尾 # 对于格式有要求 # 创建配置文档 ...

  5. 面向对象总结、configparser配置文件模块、logging日志模块

    面向对象总结 # 学习态度# python基础 2个月# html css js jq 1个月 # 上课困 # 学习方法 :# 列出知识点# 例子 写了哪些 # 面向对象学了哪些块# 为什么要讲面向对 ...

  6. Python之配置文件模块 ConfigParser

    写项目肯定用的到配置文件,这次学习一下python中的配置文件模块 ConfigParser 安装就不说了,pip一下即可,直接来个实例 配置文件 project.conf [db] host = ' ...

  7. s14 第5天 时间模块 随机模块 String模块 shutil模块(文件操作) 文件压缩(zipfile和tarfile)shelve模块 XML模块 ConfigParser配置文件操作模块 hashlib散列模块 Subprocess模块(调用shell) logging模块 正则表达式模块 r字符串和转译

    时间模块 time datatime time.clock(2.7) time.process_time(3.3) 测量处理器运算时间,不包括sleep时间 time.altzone 返回与UTC时间 ...

  8. 第四十二节,configparser特定格式的ini配置文件模块

    configparser用于处理特定格式的文件,其本质上是利用open来操作文件. 特定格式的ini配置文件模块,用于处理ini配置文件,注意:这个ini配置文件,只是ini文件名称的文本文件,不是后 ...

  9. Python模块之: ConfigParser 配置文件读取

    Python模块之: ConfigParser 配置文件读取   ConfigParser用于读写类似INI文件的配置文件,配置文件的内容可组织为组,还支持多个选项值(option-value)类型. ...

随机推荐

  1. javascript--study

    1.函数传参:按值传递 对于数字.字符串等是将它们的值传递给了函数参数,函数参数的改变不会影响函数外部的变量. 对于数组和对象等是将对象(数组)的变量的值传递给了函数参数,这个变量保存的指向对象(数组 ...

  2. leetcode day8

    [83] Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that ...

  3. dev repositoryItem 手工定义

    一.打开设计界面 二.定义Repository 事件定义 三.把repositoryItemTextEdit1邦定存在的列

  4. 绘图——Android绘图基础:Canvas、Paint等

    Android的绘图应该继承View组件,并重写它的onDraw(Canvas canvas)方法即可. 重写onDraw(Canvas canvas)方法时涉及一个绘图API:Canvas,Canv ...

  5. Angular - - $compile编译服务与指令

    $compile 这是个编译服务.编译一段HTML字符串或者DOM的模板, 产生一个将scope和模板连接到一起的函数. 编译服务主要是为指令编译DOM元素,下面的一大段也是主要介绍指令的. 下面是一 ...

  6. Brackets - 强大免费的开源跨平台Web前端开发工具IDE (HTML/CSS/Javascript代码编辑器)

    Brackets 是一个免费.开源且跨平台的 HTML/CSS/JavaScript 前端 WEB 集成开发环境 (IDE工具).该项目由 Adobe 创建和维护,根据MIT许可证发布,支持 Wind ...

  7. 【翻译】使用Visual Studio在Azure上部署Asp.Net Core Web应用

    配置运行环境 Install the latest Azure SDK for Visual Studio. The SDK installs Visual Studio if you don't a ...

  8. 进度条(ProgressBar)的功能与用法

    进度条也是UI界面中一种非常实用的组件,通常用于向用户显示某个耗时操作完成的的百分比.进度条可以动态的显示进度,因此避免长时间的执行某个耗时的操作,让用户感觉程序失去了响应,从而更好的提高用户界面的友 ...

  9. 第一部分 代码组织概念,集成开发环境(IDE)

    代码组织概念 主要是代码文件,项目和解决方案. 解决方案(.sln)包含多个项目(.csproj),一个项目又包含多个文件(.cs). 集成开发环境(IDE): 由编辑.编译.调试,以及用户图形界面, ...

  10. 11g oracle 用户密码过期问题

    Oracle 11g 之前默认的用户时是没有密码过期的限制的,在Oracle 11g 中默认的profile启用了密码过期时间是180天.如下:select * from dba_profiles w ...