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模块的更多相关文章

  1. Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)

    本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 一.前言 我们在<中我们描述了Python数据持久化的大体概念和基本处理方式,通过这些知识点我们已经 ...

  2. 【转】Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)

    [转]Python之xml文档及配置文件处理(ElementTree模块.ConfigParser模块) 本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 ...

  3. Python常用内置模块之xml模块

    xml即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言.从结构上,很像HTML超文本标记语言.但他们被设计的目的是不同的,超文本标记语言被设计用来显示 ...

  4. python基础14 ---函数模块4(configparser模块)

    configparser模块 一.configparser模块 1.什么是configparser模块:configparser模块操作配置文件,配置文件的格式与windows ini和linux的c ...

  5. [xml模块、hashlib模块、subprocess模块、os与sys模块、configparser模块]

    [xml模块.hashlib模块.subprocess模块.os与sys模块.configparser模块] xml模块 XML:全称 可扩展标记语言,为了能够在不同的平台间继续数据的交换,使交换的数 ...

  6. 《Python》hashlib模块、configparser模块、logging模块

    一.hashlib模块 Python的hashlib模块中提供了常见的摘要算法,如md5,sha1等等. 摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定的字符串(通 ...

  7. python 之 subprocesss 模块、configparser 模块

    6.18 subprocesss 模块 常用dos命令: cd : changedirectory 切换目录 ​ tasklist:查看任务列表 ​ tasklist | findstr python ...

  8. 小白的Python之路 day5 configparser模块的特点和用法

    configparser模块的特点和用法 一.概述 主要用于生成和修改常见配置文件,当前模块的名称在 python 3.x 版本中变更为 configparser.在python2.x版本中为Conf ...

  9. Python之路(第十八篇)shutil 模块、zipfile模块、configparser模块

    一.shutil 模块 1.shutil.copyfileobj(fsrc, fdst[, length]) 将文件内容拷贝到另一个文件中,需要打开文件 import shutil shutil.co ...

随机推荐

  1. CentOS7安装GNOME可视化界面

    1.首先安装X(X Window System),命令为 yum groupinstall "X Window System" 回车(注意有引号)   1CentOS Linux系 ...

  2. SpringBoot 简单集成ActiveMQ

    ActiveMQ安装配置步骤见:https://www.cnblogs.com/vincenshen/p/10635362.html 第一步,pom.xml引入ActiveMQ依赖 <depen ...

  3. Quick Search FAQ

    Q: Why does it jump to an incorrect page? A: Some categories, such as twitter, may need to log in in ...

  4. eclipse 工程没有build path

    项目的.project文件添加: <buildSpec><buildCommand><name>org.eclipse.jdt.core.javabuilder&l ...

  5. Nginx配置X-Forwarded-Proto

    需求 最近公司在做全站https,架构上面有Nginx+tomcat Nginx+php,且nginx配置了ssl,tomcat和php项目使用https协议 但是,发送的是https url请求,p ...

  6. TSP - 状态压缩dp

    2017-08-11 21:10:21 艾教写的 #include<iostream> #include<cstdio> #include<cstring> #in ...

  7. .net 下的 HttpRuntime.Cache 应用

    using System;using System.Collections.Generic;using System.Diagnostics;using System.Linq;using Syste ...

  8. 一、nginx 安装

    添加官方 yum 源 vim /etc/yum.repos.d/nginx.rep 输入以下内容(OS为你的系统,OSRELEASE 系统版本) [nginx] name=nginx repo bas ...

  9. opencv图像处理

    #coding=utf-8 import cv2 import numpy as np img1 = cv2.imread("3.jpg") img2 = cv2.imread(& ...

  10. filter-mapping中的dispatcher使用

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGAAAAEJCAIAAABUr8bLAAAgAElEQVR4nO3dX2/bVoL3cb4h+WYnwN