configparser模块用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser。

首先要写一个如下所示的配置文件:

[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes [bitbucket.org]
user = hg [topsecret.server.com]
host port = 50022
forwardx11 = no

先分析配置文件的内容,内容的格式就像是字典中的键值对一样,所以写配置文件的方法就是用到了字典,如下所示:

# Author:南邮吴亦凡

# 在配置文件中的操作就相当于是在操作字典

import configparser  # python2中是ConfigParser

config = configparser.ConfigParser()

# 第一个节点:在配置文件中的DEFAULT部分的内容
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['Host Port'] = '50022' # mutates the parser
topsecret['ForwardX11'] = 'no' # same here
config['DEFAULT']['ForwardX11'] = 'yes'
with open('example_1.ini', 'w') as configfile:
config.write(configfile)

下面是是生成的文件内容;



下面主要介绍配置文件的“读”:

我导入了刚刚创建的配置文件,并且读取其中的内容,

import configparser

conf = configparser.ConfigParser()
conf.read("example_1.ini") print(conf.sections()) # DEFAULT部分的内容不会显示出来,因为他是配置文件中默认的部分
print(conf.defaults())
print(conf["bitbucket.org"]) # 和字典的读法一样
print(conf["bitbucket.org"]["user"])

读取结果如下所示:

python中configparser模块的使用的更多相关文章

  1. python中confIgparser模块学习

    python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...

  2. Python中ConfigParser模块应用

    Python中ConfigParser模块应用 Python的ConfigParser模块定义了3个对INI文件进行操作的类 RawConfigParser.ConfigParser和SafeConf ...

  3. python中configparser模块读取ini文件

    python中configparser模块读取ini文件 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(se ...

  4. python中configparser模块

    python中的configparse模块的使用 主要用来解析一些常用的配置,比如数据配置等. 例如:有一个dbconfig.ini的文件 [section_db1] db = test_db1 ho ...

  5. python中configparser模块记录

    python中用来读取配置文件,配置文件的格式相同于windows下的ini配置文件 一.常用函数 read(filename) #读取配置文件,直接读取ini文件内容 sections() #获取i ...

  6. python封装configparser模块获取conf.ini值(优化版)

    昨天晚上封装了configparser模块,是根据keyname获取的value.python封装configparser模块获取conf.ini值 我原本是想通过config.ini文件中的sect ...

  7. (转)python的ConfigParser模块

    原文:https://blog.csdn.net/miner_k/article/details/77857292 如何使用Python3读写INI配置文件-------https://blog.cs ...

  8. python 的ConfigParser模块

    Python 之ConfigParser模块 一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.sect ...

  9. Python中optionParser模块的使用方法[转]

    本文以实例形式较为详尽的讲述了Python中optionParser模块的使用方法,对于深入学习Python有很好的借鉴价值.分享给大家供大家参考之用.具体分析如下: 一般来说,Python中有两个内 ...

随机推荐

  1. centos 设置时间为北京时间

    https://www.cnblogs.com/biaopei/p/7730462.html

  2. Item的anchors属性

    1.anchors group: anchors.top : AnchorLine anchors.bottom : AnchorLine anchors.left : AnchorLine anch ...

  3. 大数乘法|2012年蓝桥杯B组题解析第六题-fishers

    (9')大数乘法 对于32位字长的机器,大约超过20亿,用int类型就无法表示了,我们可以选择int64类型,但无论怎样扩展,固定的整数类型总是有表达的极限!如果对超级大整数进行精确运算呢?一个简单的 ...

  4. P3833 [SHOI2012]魔法树

    思路 树剖板子 注意给出点的编号是从零开始的 代码 #include <cstdio> #include <algorithm> #include <cstring> ...

  5. (zhuan) 资源|TensorFlow初学者必须了解的55个经典案例

    资源|TensorFlow初学者必须了解的55个经典案例 2017-05-27 全球人工智能 >>>>>>欢迎投稿:news@top25.cn<<< ...

  6. %lld 和 %I64d的区别

    参考一个博客的链接:https://blog.csdn.net/thunders01/article/details/38879553

  7. HDU 4918 Query on the subtree(动态点分治+树状数组)

    题意 给定一棵 \(n\) 个节点的树,每个节点有点权.完成 \(q\) 个操作--操作分两种:修改点 \(x\) 的点权.查询与 \(x\) 距离小于等于 \(d\) 的权值总和. \(1 \leq ...

  8. @PathVariable与@RequestBody的区别,及前段请求接口的写法。

    @PathVariable 1:接受参数时,地址栏为:/{args1}/{args2} 2:用法:(@PathVariable(value = "args")Long  id) 3 ...

  9. GET和POST中文乱码的解决方法

    如果表单中含有中文,采用GET或者POST提交请求时,getParameter()方法接收到的参数值乱码. 1.乱码产生的原因 请求参数通过浏览器发送给Tomcat服务器,浏览器发送编码,但是tomc ...

  10. 【.NET开发之美】使用ComponentOne提高.NET DataMap中的加载速度

    概述 FlexGrid for WinForm 采用了最新的数据绑定技术,并与Microsoft .NET Framework无缝集成. 因此,您可以获得易于使用的灵活网格控件,用于创建用户友好界面, ...