python 之 configparser 模块

[[email protected] python]# vim config.py

import configparser

config = configparser.ConfigParser()
config["DEFAULT"] = {‘ServerAliveInterval‘: ‘45‘,
                     ‘Compression‘: ‘yes‘,
                     ‘CompressionLevel‘: ‘9‘}
config[‘bitbucket.org‘] = {}
config[‘bitbucket.org‘][‘User‘] = ‘Root‘

config[‘topsecret.server.com‘] = {}
topsecret = config[‘topsecret.server.com‘]
topsecret[‘Host Port‘] = ‘5200‘
topsecret[‘Forward‘] = ‘yes‘

config[‘DEFAULT‘][‘Forward‘] = ‘No‘

with open(‘example.ini‘,‘w‘) as configfile:
  config.write(configfile)

config.set(‘topsecret.server.com‘,‘Hosr Port‘,‘5601‘)  #改

config.remove_section(‘topsecret.server.com‘) #删
config.write(open (‘xml.ini‘,‘w‘))

[[email protected] python]# python config.py

[[email protected] python]# cat example.ini
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forward = No

[bitbucket.org]
user = Root

[topsecret.server.com]
host port = 5200
forward = yes

[[email protected] python]# cat xml.ini
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forward = No

[bitbucket.org]
user = Root

原文地址:https://www.cnblogs.com/lixinliang/p/8538607.html

时间: 2024-07-30 04:58:20

python 之 configparser 模块的相关文章

Python中ConfigParser模块应用

Python中ConfigParser模块应用 Python的ConfigParser模块定义了3个对INI文件进行操作的类 RawConfigParser,ConfigParser和SafeConfigParser.其中RawCnfigParser是最基础的INI文件读取类,ConfigParser.SafeConfigParser支持对%(value)s变量的解析. 下面看看怎样通过ConfigParser类来解析一个ini文件. 配置文件settings.cfg [DEFAULT] myk

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

昨天晚上封装了configparser模块,是根据keyname获取的value.python封装configparser模块获取conf.ini值 我原本是想通过config.ini文件中的section和keyname获取value的,前两天怎么都调试不通过.今天百度了一下,有人通过字典的方式把我的和这个想法实现了,我把这个例子修改了一下,代码如下,并通过测试,以后可以用在自动化测试框架中: 1 #coding:utf-8 2 import os 3 import ConfigParser

python的ConfigParser模块

简介 ConfigParser模块在python3中修改为configparser.这个模块定义了一个ConfigParser类,该类的作用是使用配置文件生效,配置文件的格式和windows的INI文件的格式相同 该模块的作用 就是使用模块中的RawConfigParser().ConfigParser(). SafeConfigParser()这三个方法(三者择其一),创建一个对象使用对象的方法对指定的配置文件做增删改查 操作. 配置文件有不同的片段组成和Linux中repo文件中的格式类似:

python 之ConfigParser模块学习

1.1 读取配置文件 -read(filename) 直接读取ini文件内容 -sections() 得到所有的section,并以列表的形式返回 -options(section) 得到该section的所有option -items(section) 得到该section的所有键值对 -get(section,option) 得到section中option的值,返回为string类型 -getint(section,option) 得到section中option的值,返回为int类型 1

python之configParser模块读写配置文件

借鉴:http://www.cnblogs.com/TankXiao/p/3038350.html configParser是python自带的模块,用来读写配置文件 配置文件的格式:[]包含的叫section,section下有option=value这样的键值 配置文件  test.conf [section1] name = tank age = 28 [section2] ip = 127.0.0.1 port = 8080 python代码 #-*- coding:UTF-8 -*-

python之configparser模块

1. 简介 configparser用于配置文件解析,可以解析特定格式的配置文件,多数此类配置文件名格式为XXX.ini,例如mysql的配置文件.在python3.X中 模块名为configparser ,在python2.X中使用的模块名为ConfigParser. ##### ini 文件示例 ######## [section1] name = wang age = 18 [section2] name:python age = 19 #### 文件格式说明 ######### [XXX

Python基础-ConfigParser模块

此模块用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 来看一个好多软件的常见配置文件格式如下 [DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 ForwardX11 = yes [bitbucket.org] User = hg [topsecret.server.com] Port = 50022 ForwardX11 = no 解析配

python之ConfigParser模块处理ini文件

ini文件 [global]      #######global为session  IPADDR为option    115.182.1.157为值 IPADDR = 115.182.1.157        ######为键值对 IPADDR2 = 10.0.3.157 [7192] mysql = 3306 port = 80 mem = 90% load = 2 inode = 90% disk = 90% 详细介绍 >>> import ConfigParser     ###

python:实例化configparser模块读写配置文件

之前的博客介绍过利用python的configparser模块读写配置文件的基础用法,这篇博客,介绍下如何实例化,方便作为公共类调用. 实例化的好处有很多,既方便调用,又降低了脚本的维护成本,而且提高了代码的可读性... 1.配置文件 configparser模块支持读取.conf和.ini等类型的文件,在文件夹新建一个.ini文件,写入一些信息,示例如下: config.ini [driver] chromedriver = E:\automation\UI\Testcase\browser\