主要内容来自景女神博客

内涵:该模块适用于配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键=值)。

常见文档格式:

 [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["DEFAULT"] = {'ServerAliveInterval': '',
'Compression': 'yes',
'CompressionLevel': '',
'ForwardX11':'yes'
} config['bitbucket.org'] = {'User':'hg'} config['topsecret.server.com'] = {'Host Port':'','ForwardX11':'no'} with open('example.ini', 'w') as configfile: config.write(configfile)

查找文件:

 import configparser

 config = configparser.ConfigParser()

 #---------------------------查找文件内容,基于字典的形式

 print(config.sections())        #  []

 config.read('example.ini')

 print(config.sections())        #   ['bitbucket.org', 'topsecret.server.com']

 print('bytebong.com' in config) # False
print('bitbucket.org' in config) # True print(config['bitbucket.org']["user"]) # hg print(config['DEFAULT']['Compression']) #yes print(config['topsecret.server.com']['ForwardX11']) #no print(config['bitbucket.org']) #<Section: bitbucket.org> for key in config['bitbucket.org']: # 注意,有default会默认default的键
print(key) print(config.options('bitbucket.org')) # 同for循环,找到'bitbucket.org'下所有键 print(config.items('bitbucket.org')) #找到'bitbucket.org'下所有键值对 print(config.get('bitbucket.org','compression')) # yes get方法Section下的key对应的value
复制代码

增删改查操作:

 import configparser

 config = configparser.ConfigParser()

 config.read('example.ini')

 config.add_section('yuan')

 config.remove_section('bitbucket.org')
config.remove_option('topsecret.server.com',"forwardx11") config.set('topsecret.server.com','k1','')
config.set('yuan','k2','') config.write(open('new2.ini', "w"))

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

  1. configparser 配置文件模块

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

  2. configparser配置文件模块

    1.configparser的作用 mysql等很多文件的配置如下: [DEFAULT]ServerAliveInterval = 45Compression = yesCompressionLeve ...

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

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

  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. sql server 查询数据判断为空

    and xxx is NOT null and xxx is null

  2. mui scrollTo到指定位置,出现空白页及拉不动的问题解决

    使用方式简介 mui 列表页使用的是 mui的插件实现的上拉加载下拉刷新,但是从详情页回到列表页时 不能回到之前的位置.所以想到了使用缓存. 第一次和第二次的试验是失败的.失败后,就想用其他办法来解决 ...

  3. 移动APP 微信支付完整过程(wxPay 方案一)

    apicloud.weixinpay官方提供了两种方案. 本模块封装了两套支付方案: 方案一:开发者通过 getOrderId.payOrder 自己处理签名过程(微信开放平台建议把 getOrder ...

  4. 填坑...P1546 最短网络 Agri-Net

    P1546 最短网络 Agri-Net 难度普及/提高- 时空限制1s / 128MB 题目背景 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当然,他需要 ...

  5. [cf 1015f] Bracket Substring (dp+kmp)

    传送门 Solution 设dp方程dp[now][pos][red][fla]表示还有now个位置,pos表示匹配到第几位,red表示左括号数-右括号数,fla表示是否已经是给定串的字串 暴力转移即 ...

  6. phpcms v9 邓士鹏(石家庄职业技术学院)

    头部标题.关键词.描述调用: <title>{if isset($SEO['title']) && !empty($SEO['title'])}{$SEO['title'] ...

  7. Flask中的session操作

    一.配置SECRET_KEY 因为flask的session是通过加密之后放到了cookie中.所以有加密就有密钥用于解密,所以,只要用到了flask的session模块就一定要配置“SECRET_K ...

  8. [bzoj1090][SCOI2003]字符串折叠_区间dp

    字符串折叠 bzoj-1090 SCOI-2003 题目大意:我说不明白...链接 注释:自己看 想法:动态规划 状态:dp[i][j]表示从第i个字符到第j个字符折叠后的最短长度. 转移:dp[l] ...

  9. Spring MVC-页面重定向示例(转载实践)

    以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_page_redirection.htm 说明:示例基于Spring MVC 4. ...

  10. mysql稳定的版本号选择及下载说明(2014-11-10)

    怎样选择新稳定的版本号       mysql的版本号大概能够分为Alpha.Beta.GA. GA版即mysql官方公布的稳定版本号. 怎样在官方下载Mysql        能够通过http:// ...