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生成配置文件

import configparser

config = configparser.ConfigParser()
config["DEFAULT"] = {'ServerAliveInterval': '',
'Compression': 'yes',
'CompressionLevel': ''} config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '' # mutates the parser
topsecret['ForwardX11'] = 'no' # same here
config['DEFAULT']['ForwardX11'] = 'yes'
with open('example.ini', 'w') as configfile:
config.write(configfile)

读取配置文件信息

#!/usr/bin/env python3
# -*- coding:utf-8 -*- import configparser config = configparser.ConfigParser()
config.sections() config.read('example') config.sections() for i in config.keys():
print(i, end=' ')
print("=" * 50)
print(config.keys()) print(config['bitbucket.org']['User']) print(config['DEFAULT']['Compression']) topsecret = config['topsecret.server.com']
print(topsecret['ForwardX11'])
print(topsecret['Port']) for key in config['bitbucket.org']:
print(key) print(config['bitbucket.org']['ForwardX11'])

configparser增删改查语法

 
########### i.cfg file ##########
[section1]
k1 = v1
k2:v2
k5 : 10 [section2]
k3 = v3
k4 = v4
age : 88
#################################
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# author: songwei import configparser # ########## 读 ########## config = configparser.ConfigParser()
config.read('example') secs = config.sections()
print(secs) #--->['section1', 'section2']
options = config.options('section2')
print(options) #--->['k3', 'k4', 'age'] item_list = config.items('section2')
print(item_list) #--->[('k3', 'v3'), ('k4', 'v4'), , ('age', '88')] val = config.get("section1", "k2")
print(val) #--->v2
val2 = config.getint('section1','k5')
print(val2) #---> 10 # ########## 改写 ########## config = configparser.ConfigParser()
config.read('example')
sec = config.remove_section('section1') #删除
config.write(open('i.cfg', "w")) sec = config.has_section('wupeiqi')
print(sec)
sec = config.add_section('wupeiqi')
print(sec)
config.write(open('i.cfg', "w")) config.set('section2','k4',"")
# config.defaults()
config.write(open('i.cfg', "w")) # config.remove_option('section2','age')
# config.write(open('i.cfg', "w"))

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

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

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

  6. Python自动化测试 -ConfigParser模块读写配置文件

    C#之所以容易让人感兴趣,是因为安装完Visual Studio, 就可以很简单的直接写程序了,不需要做如何配置. 对新手来说,这是非常好的“初体验”, 会激发初学者的自信和兴趣. 而有些语言的开发环 ...

  7. Python操作配置文件configparser模块

    在实际的开发过程中,我们常有操作ini格式和conf格式配置文件的操作,Python为我们提供了configparser模块,方便我们对配置文件进行读写操作. config.ini配置文件内容如下: ...

  8. Python使用ConfigParser模块读取配置文件(config.ini)以及写入配置文件

    前言 使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是configParser.configPars ...

  9. python中configparser模块的使用

    configparser模块用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 首先要写一个如下所示的配置文件: [DEFAULT] serv ...

随机推荐

  1. 10.06 WZZX Day1总结

    今天迎来了WZZX的模拟.打开pdf的时候我特别震惊,出题的竟然是神仙KCZ!没错,就是那个活跃于各大OJ,在各大OJ排名靠前(LOJ Rank1),NOI2018 Rank16进队的kczno1!! ...

  2. Windows路径

    绝对路径 是从盘符开始的路径,形如 C:\windows\system32\cmd.exe 相对路径 是从当前目录开始的路径,假如当前目录为C:\windows 要描述上述路径,只需输入 system ...

  3. CF 600 E Lomsat gelral —— 树上启发式合并

    题目:http://codeforces.com/contest/600/problem/E 看博客:https://blog.csdn.net/blue_kid/article/details/82 ...

  4. request.getRemoteAddr()和request.getRemoteHost()

    转自:https://www.cnblogs.com/aizj/p/7593209.html request.getRemoteAddr()是获得客户端的ip地址.request.getRemoteH ...

  5. JS自定义右键菜单案例

    要求:点击页面鼠标右键,阻止默认右键菜单的弹出,并弹出自定义右键菜单. 效果示例: 参考代码: <!DOCTYPE html> <html lang="zh-CN" ...

  6. linux学习之路1 Linux系统安装

    VMware workstation虚拟器 网上下载VMware workstation,然后安装任一系统的linux系统,不过选的系统一定要跟你下载好的linux镜像保持一致,博主装的是Red Ha ...

  7. 373 Find K Pairs with Smallest Sums 查找和最小的K对数字

    给定两个以升序排列的整形数组 nums1 和 nums2, 以及一个整数 k.定义一对值 (u,v),其中第一个元素来自 nums1,第二个元素来自 nums2.找到和最小的 k 对数字 (u1,v1 ...

  8. maven build过程中遇到的问题以及解决方案

    (1)不支持泛型以及@Override 问题来源:使用了低版本的jdk,默认情况下maven使用的是jdk1.5的版本,而泛型和@Override是后期版本才有的,需要更改maven默认的jdk版本. ...

  9. ListView用法

    public class MainActivity extends Activity implements OnItemClickListener, OnScrollListener { privat ...

  10. 浏览器的两种模式quirks mode 和strict mode

    关键字: javascript.quirks mode.strict mode 在看js代码时,有时会看到关于quirks mode(怪异模式)和strict mode(严格格式)的东西,一直也没深究 ...