以前傻傻的不知道还有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. Gradle 下载不了

    可自行下载对应的 gradle-x.x-all.zip 放在下列目录 C:\Users\penno\.gradle\wrapper\dists\gradle-4.4-all\9br9xq1tocpiv ...

  2. Codeforces Round #224 (Div. 2) A. Ksenia and Pan Scales

    A. Ksenia and Pan Scales time limit per test 1 second memory limit per test 256 megabytes input stan ...

  3. Cannot find config.m4. Make sure that you run '/usr/local/php/bin/phpize' in the top level source directory of the module的 解决方法

    cp /php-7.1.22/ext/openssl/config0.m4 /usr/local/php/bin/config.m4

  4. POJ3669解题报告(bfs)

    POJ3669http://poj.org/problem?id=3669 很明显是一道bfs的题目 由于陨石的降临具有时刻性,所以地图是随时间变化的, 所以可以使用结构体来存储陨石下落的时刻以及位置 ...

  5. 《DSP using MATLAB》Problem 4.10

    今天擦完了玻璃,尽管有地方不那么明亮干净,冷风中瑟瑟发抖,年也快临近了. 代码是从网上找的, function [p, np, r, nr] = deconv_m(b, nb, a, na) % Mo ...

  6. java内存的分配和管理

    常用的三个内存空间 栈内存 ,堆内存 ,方法区 栈内存存储的内容: 局部变量. 函数(栈中的局部变量,需要手动赋值.当变量,或者函数执行完毕,就自动被释放) 堆内存,存储的内容 :全局变量.数据容器. ...

  7. java数据结构链表(纯面向对象的方式)

    //Node.java //LinkedList.java _____ //测试类 [lcy, haha]    1  lcy 删除的对象是 1[lcy, haha] lcy

  8. openresty 使用cuid 类库生成短链接id

    cuid 是一个不错的id 生成算发,类似的有shortid .hashid 演示使用lua 包集成openresty 做测试 使用docker-compose 运行 dockerfile FROM ...

  9. bootstrap modal模态框的运用

    http://www.ziqiangxuetang.com/bootstrap/bootstrap-modal-plugin.html 方法 下面是一些可与 modal() 一起使用的有用的方法. 方 ...

  10. MessageRPC

    项目地址 :  https://github.com/kelin-xycs/MessageRPC MessageRPC 一个 用 C# 实现的 使用 Message 的 RPC MessageRPC ...