python常用模块之configparser模块
python常用模块之configparser
作用:解析配置文件
假设在当前目录下有这样一个conf.ini文件
[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes
[bitbucket.org]
User = hg
[topsecret.server.com]
Port = 50022
ForwardX11 = no
模块的操作
import configparser
conf = configparser.ConfigParser() # 创建一个对象
# print(conf.sections()) # [],因为没有打开文件,所以是空的
conf.read("conf.ini") # 读取文件内容
print(conf.sections()) # ['bitbucket.org', 'topsecret.server.com']
# 那么为什么没有DEFAULT呢?因为在每一个配置文件中都会有一个DEFAULT,这是全局默认配置的东西,打印不出来的,但是可以获取到
print(conf.default_section) # DEFAULT
# 拿到里面的值
print(conf['bitbucket.org']['User']) # hg 此时是知道这个配置文件中的子模块bitbucket.org里有User
# 循环
for k,v in conf['bitbucket.org'].items():
print(k,v)
# user hg
# serveraliveinterval 45
# compression yes
# compressionlevel 9
# forwardx11 yes
那么,为啥会把DEFAULT里的打印出来呢?因为这是configparser设置的,会默认出现在每一个节点中
configparser其他的操作
# 还是以上面的conf.ini为例
import configparser
conf = configparser.ConfigParser() # 生成一个对象
conf.read("conf.ini",encoding='utf-8') # 读取配置文件内容
# 读
# print(dir(conf))
print(conf.options("bitbucket.org")) # 将bitbucket.org区域里的key全部拿出,包括DEFAULT里面的,['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11']
print(conf['bitbucket.org']['User']) # hg,拿到bitbucket.org里的User这个key的值
# 增加
conf.add_section("group1") # 增加name区域
conf['group1']['age'] = '22' # 增加group1区域中age这个key的值为22
conf['group1']['name'] = 'xiao'
conf.write(open("conf.ini","r+")) # 写进文件中
conf.write(open("i.cfg","w")) # 或者写到一个新文件中
# 删除
# conf.remove_section('group1') # 删除整个group1区域
# conf.write(open('i.cfg','w'))
conf.remove_option('group1','name') # 只删除group1区域里的name这个key
conf.write(open('conf.ini','w'))
python常用模块之configparser模块的更多相关文章
- Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)
本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 一.前言 我们在<中我们描述了Python数据持久化的大体概念和基本处理方式,通过这些知识点我们已经 ...
- 【转】Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)
[转]Python之xml文档及配置文件处理(ElementTree模块.ConfigParser模块) 本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 ...
- Python常用内置模块之xml模块
xml即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言.从结构上,很像HTML超文本标记语言.但他们被设计的目的是不同的,超文本标记语言被设计用来显示 ...
- python基础14 ---函数模块4(configparser模块)
configparser模块 一.configparser模块 1.什么是configparser模块:configparser模块操作配置文件,配置文件的格式与windows ini和linux的c ...
- [xml模块、hashlib模块、subprocess模块、os与sys模块、configparser模块]
[xml模块.hashlib模块.subprocess模块.os与sys模块.configparser模块] xml模块 XML:全称 可扩展标记语言,为了能够在不同的平台间继续数据的交换,使交换的数 ...
- 《Python》hashlib模块、configparser模块、logging模块
一.hashlib模块 Python的hashlib模块中提供了常见的摘要算法,如md5,sha1等等. 摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定的字符串(通 ...
- python 之 subprocesss 模块、configparser 模块
6.18 subprocesss 模块 常用dos命令: cd : changedirectory 切换目录 tasklist:查看任务列表 tasklist | findstr python ...
- 小白的Python之路 day5 configparser模块的特点和用法
configparser模块的特点和用法 一.概述 主要用于生成和修改常见配置文件,当前模块的名称在 python 3.x 版本中变更为 configparser.在python2.x版本中为Conf ...
- Python之路(第十八篇)shutil 模块、zipfile模块、configparser模块
一.shutil 模块 1.shutil.copyfileobj(fsrc, fdst[, length]) 将文件内容拷贝到另一个文件中,需要打开文件 import shutil shutil.co ...
随机推荐
- markdown工作随笔总结
1. 锚点 (使用方法和链接很像) ## 目录 1. [命名](#命名) ....... **[返回顶部](#目录)** ## 命名 ###命名原则 可以从返回顶部回到目录,也可以点击目录的命名跳到命 ...
- [翻译]小提示:使用figure和figcaption元素的正确方式
figure和figcaption是一对经常被一起使用的语义化标签.如果你还没有看过规范中的定义,现在有机会在你的项目中使用它们了.如果你不知道怎么用,下面是关于如何正确使用它们的一些提示. figu ...
- git将本地已经存在的分支和一个指定的远端分支建立映射关系
Make an existing Git branch track a remote branch? Given a branch foo and a remote upstream: As of G ...
- LeetCode——Find All Duplicates in an Array
Question Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice an ...
- STL map用法总结(multimap)
2017-08-19 10:58:52 writer;pprp #include <map> #include <string> #include <iostream&g ...
- POJ 2299 Ultra-QuickSort(树状数组+离散化)
http://poj.org/problem?id=2299 题意:给出一组数,求逆序对. 思路: 这道题可以用树状数组解决,但是在此之前,需要对数据进行一下预处理. 这道题目的数据可以大到999,9 ...
- POJ 3352 Road Construction(边—双连通分量)
http://poj.org/problem?id=3352 题意: 给出一个图,求最少要加多少条边,能把该图变成边—双连通. 思路:双连通分量是没有桥的,dfs一遍,计算出每个结点的low值,如果相 ...
- 关于MAC升级后,vim更新插件报错
找不到路径: 直接在终端 terminal 输入: xcode-select --install
- Java_SQL_类型对应_资料
1.http://argel-lj.iteye.com/blog/1183123 2.http://www.fx114.net/qa-119-110105.aspx JDBC 的"类型&qu ...
- ios 下拉刷新开源框架 MJRefresh
gitHub 下载框架 搜索MJExampleViewController.h 下拉刷新 MJTableViewController 上拉刷新 MJTableViewController Collec ...