python之ConfigParser的使用。
ConfigParser 是用来读取配置文件的包。配置文件的格式如下:中括号“[ ]”内包含的为section。section 下面为类似于key-value 的配置内容。
2: db_host = 127.0.0.1
3: db_port = 22
4: db_user = root
5: db_pass = rootroot
6:
7: [concurrent]
8: thread = 10
9: processor = 20
中括号“[ ]”内包含的为section。紧接着section 为类似于key-value 的options 的配置内容。
二、ConfigParser 初始工作
使用ConfigParser 首选需要初始化实例,并读取配置文件:
2: cf.read("配置文件名")
三、ConfigParser 常用方法
1. 获取所有sections。也就是将配置文件中所有“[ ]”读取到列表中:
2: print 'section:', s
将输出(以下将均以简介中配置文件为例):
2. 获取指定section 的options。即将配置文件某个section 内key 读取到列表中:
2: print 'options:', o
将输出:
3. 获取指定section 的配置信息。
2: print 'db:', v
将输出:
4. 按照类型读取指定section 的option 信息。
同样的还有getfloat、getboolean。
2: db_host = cf.get("db", "db_host")
3: db_port = cf.getint("db", "db_port")
4: db_user = cf.get("db", "db_user")
5: db_pass = cf.get("db", "db_pass")
6:
7: # 返回的是整型的
8: threads = cf.getint("concurrent", "thread")
9: processors = cf.getint("concurrent", "processor")
10:
11: print "db_host:", db_host
12: print "db_port:", db_port
13: print "db_user:", db_user
14: print "db_pass:", db_pass
15: print "thread:", threads
16: print "processor:", processors
将输出:
2: db_port: 22
3: db_user: root
4: db_pass: rootroot
5: thread: 10
6: processor: 20
5. 设置某个option 的值。(记得最后要写回)
2: cf.write(open("test.conf", "w"))
6.添加一个section。(同样要写回)
2: cf.set('liuqing', 'int', '15')
3: cf.set('liuqing', 'bool', 'true')
4: cf.set('liuqing', 'float', '3.1415')
5: cf.set('liuqing', 'baz', 'fun')
6: cf.set('liuqing', 'bar', 'Python')
7: cf.set('liuqing', 'foo', '%(bar)s is %(baz)s!')
8: cf.write(open("test.conf", "w"))
7. 移除section 或者option 。(只要进行了修改就要写回的哦)
2: cf.remove_section('liuqing')
3: cf.write(open("test.conf", "w"))
点击(此处)折叠或打开
- #!/usr/bin/env
python - from ConfigParser import ConfigParser
- CONFIGFILE="f.txt"
- config=ConfigParser()
- config.read(CONFIGFILE)
- print config.get('messages','greeting')
- radius=input(config.get('messages','questions')+'
') - print config.get('messages','result')
- print config.getfloat('numbers','pi')*radius**2
- s=config.sections()
- print'section:
',s - o=config.options('messages')
- print'messages
option: ',o - v=config.items("messages")
- print'message
de xinxi: ',v - config.add_section('liuyang1')
- config.set('liuyang1','int','15')
- config.set('liuyang'1,'hhhh','hello
world') - config.write(open("f.txt","w"))
- print config.get('liuyang1','int')
- print config.get('liuyang1','hhhh')
- #!/usr/bin/env
python - import ConfigParser
- import sys
- config=ConfigParser.ConfigParser()
- config.add_section("book1")
- config.set("book1","title","hello
world") - config.set("book1","aut","log")
- config.write(open("f.txt","w"))
python之ConfigParser的使用。的更多相关文章
- python封装configparser模块获取conf.ini值(优化版)
昨天晚上封装了configparser模块,是根据keyname获取的value.python封装configparser模块获取conf.ini值 我原本是想通过config.ini文件中的sect ...
- python中confIgparser模块学习
python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...
- Python中ConfigParser模块应用
Python中ConfigParser模块应用 Python的ConfigParser模块定义了3个对INI文件进行操作的类 RawConfigParser.ConfigParser和SafeConf ...
- python中configparser模块读取ini文件
python中configparser模块读取ini文件 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(se ...
- python 的ConfigParser模块
Python 之ConfigParser模块 一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.sect ...
- 记一次用python 的ConfigParser读取配置文件编码报错
记一次用python 的ConfigParser读取配置文件编码报错 ...... raise MissingSectionHeaderError(fpname, lineno, line)Confi ...
- python中configparser模块
python中的configparse模块的使用 主要用来解析一些常用的配置,比如数据配置等. 例如:有一个dbconfig.ini的文件 [section_db1] db = test_db1 ho ...
- python 之ConfigParser
ConfigParser 简介ConfigParser是用来操作配置文件的模块. 说明:[**]为配置文件的section,基本格式为 [section] key = valueeg: [db] db ...
- Python利用ConfigParser读取配置文件
http://www.2cto.com/kf/201108/100384.html #!/usr/bin/python # -*- coding:utf-8 -*- import ConfigPars ...
随机推荐
- 面向服务体系架构(SOA)和数据仓库(DW)的思考
摘要: 当前业界对面向服务体系架构(SOA)和数据仓库(Data Warehouse,DW)都介绍的很多,提出了很多优秀的解决方案,但是一般是把 SOA 和 DW 单独考虑,SOA 和 DW 有着共同 ...
- 树莓派连接DHT11温湿度传感器(python)
介绍 DHT11作为一个廉价配件,同时包含了温度.湿度传感器,而且,编码使用也非常简单. 本文介绍如果在树莓派中使用 DHT11,代码是Python.如果有任何疑问,欢迎在下面留言. 接线 VCC接5 ...
- codeforces上某题
一道codeforces上的题目. 题目大意: 定义有k个不同的字符的字符串为好字符串.现在给出一个字符串,求解对该字符串的每个前缀Si至少是多少个好字符串的连接,若不能由好字符串连接而成则输出-1. ...
- LVS 负载均衡原理详解
LVS简介 LVS是一个开源软件,由章文嵩博士于1998年5月创立,可以实现Linux平台下的简单负载均衡.LVS是Linux Virtual Server的简写,是一个虚拟的服务器集群系统. LVS ...
- CentOS7安装 VirtualBox虚拟机
官方地址 : https://www.virtualbox.org/wiki/Linux_Downloads 1.导入 yum 源 Oracle Linux / RHEL #cd /etc/yum. ...
- 10个超有趣的linux命令
本文展示了 10 个有趣的 Linux 动态命令,这些命令和实用功能无关,仅供娱乐!看完此文,你会对 Linux 有个全新的认识,谁说 IT 男就没有屌丝娱乐的一面呢?还等什么,就让我们开始看文章吧~ ...
- win10打不开菜单且点击通知栏无反应的解决方法
1.在键盘上按下win+R键,或在开始菜单图标上点击右键选择"运行" 2.输入powershell,按下“确定”运行 3.在窗口里输入或复制粘贴以下命令,注意只有一行: Get-A ...
- How does asp.net web api work?
https://hub.packtpub.com/working-aspnet-web-api/ https://docs.microsoft.com/en-us/aspnet/web-api/ove ...
- PBKDF2加密
password - the password that needs to be hashed. This should be convertedinto a char array before pa ...
- Java properties配置文件工具类
/* * Copyright (c) 2017. Panteng.Co.Ltd All rights reserved */ import org.apache.log4j.Logger; impor ...