以前傻傻的不知道还有configParser这么方便的模块,都是一个个的解析转换……

配置文件xxxxx

# 注释1

注释2

[section1] # 节点

k1 = v1    #

k2:v2       #

 

[section2] # 节点

k1 = v1    #

k2=['123','456']

节点必须是用[],节点下面的信息必须使用键值对

使用#和;都可以注释信息

1、获取所有节点

import configparser

config = configparser.ConfigParser()

config.read(‘xxxxx’, encoding='utf-8')

ret = config.sections()

print ret

2、获取指定节点下所有的键值对

import configparser

config = configparser.ConfigParser()

config.read(‘xxxxx’, encoding='utf-8')

ret = config.items('section1')

print ret

3、获取指定节点下所有的建

import configparser

config = configparser.ConfigParser()

config.read(‘xxxxx’, encoding='utf-8')

ret = config.options('section1')

print ret

4、获取指定节点下指定key的值

import configparser

config = configparser.ConfigParser()

config.read(‘xxxxx’, encoding='utf-8')

v = config.get('section1', 'k1')

5、检查、删除、添加节点

import configparser

config = configparser.ConfigParser()

config.read(‘xxxxx’, encoding='utf-8')

# 检查

has_sec = config.has_section('section1')

print has_sec

# 添加节点(只要进行了修改,就必须回写,不然信息不保存)

config.add_section("SEC_1")

config.write(open(‘xxxxx’, 'w'))

#文件信息被写之后,注释信息自动消失

#删除section或者option

config.remove_section("SEC_1")

config.write(open(‘xxxxx’, 'w'))

python之ConfigParser的更多相关文章

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

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

  2. python中confIgparser模块学习

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

  3. python之ConfigParser的使用。

    一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.section 下面为类似于key-value 的配置 ...

  4. Python中ConfigParser模块应用

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

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

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

  6. python 的ConfigParser模块

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

  7. 记一次用python 的ConfigParser读取配置文件编码报错

    记一次用python 的ConfigParser读取配置文件编码报错 ...... raise MissingSectionHeaderError(fpname, lineno, line)Confi ...

  8. python中configparser模块

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

  9. python 之ConfigParser

    ConfigParser 简介ConfigParser是用来操作配置文件的模块. 说明:[**]为配置文件的section,基本格式为 [section] key = valueeg: [db] db ...

  10. Python利用ConfigParser读取配置文件

    http://www.2cto.com/kf/201108/100384.html #!/usr/bin/python # -*- coding:utf-8 -*- import ConfigPars ...

随机推荐

  1. I.MX6 计算iomux Pin配置

    /********************************************************************************* * I.MX6 计算iomux P ...

  2. 【leetcode】2-AddTwoNums

    problem: Add Two Numbers 需要学习的是单向链表的基础使用:

  3. Tomcat下JSP、Servlet和JavaBean环境的配置

    经常看到jsp的初学者问tomcat下如何配置jsp.servlet和bean的问题,于是总结了一下如何tomcat下配置jsp.servlet和ben,希望对那些初学者有所帮助. 第一步:下载j2s ...

  4. 代理模式及Spring AOP (二)

    一.Spring AOP   1.1 Spring AOP 底层还是用的动态代理.如果目标对象所对应的类有接口,spring就用jdk生成代理对象: 如果目标对象所对应的类没有接口,spring就用C ...

  5. 《DSP using MATLAB》Problem 3.6

    逆DTFT定义如下: 需要求积分,

  6. && 和 || 运算

    a() && b() :如果执行a()后返回true,则执行b()并返回b的值:如果执行a()后返回false,则整个表达式返回a()的值,b()不执行: a() || b() :如果 ...

  7. hasura-graphql 集成 pipelinedb 1.0.0

    pipelinedb 1.0.0 已经是一个标准的pg 扩展了,同时以前的语法也有变动,但是集成进hasura-graphql 更方便了 使用docker-compose 运行 环境准备 docker ...

  8. 弹性盒子布局(display:flex)知识点

    设为 Flex 布局以后,子元素的float.clear和vertical-align属性将失效.

  9. php实现Facebook风格的 time ago函数

    php实现Facebook风格的 time ago函数 非常好用,只要把里面的英文替换成中文就行了 英文函数代码如下: <?php function nicetime($date) { if(e ...

  10. Angular 4 组件的生命周期

    1.组件的生命周期