You've already forked www.colben.cn
update
This commit is contained in:
169
content/post/python-cf.md
Normal file
169
content/post/python-cf.md
Normal file
@@ -0,0 +1,169 @@
|
||||
---
|
||||
title: "Python 的 ConfigParser 模块"
|
||||
date: 2019-10-30T18:04:09+08:00
|
||||
lastmod: 2019-10-30T18:04:09+08:00
|
||||
tags: ["python", "configparser"]
|
||||
categories: ["python"]
|
||||
---
|
||||
|
||||
# 样例文件
|
||||
- 该类配置文件可以包含一个或多个节(section),每个节可以有多个参数(键=值)。
|
||||
- 样例配置文件(/proj/conf/example_conf)
|
||||
```
|
||||
[book]
|
||||
title:ConfigParser模块教程
|
||||
author:大头爸爸
|
||||
email:366500050@qq.com
|
||||
time:2012-09-20 22:04:55
|
||||
[size]
|
||||
size:1024
|
||||
[other]
|
||||
blog:csdn.net
|
||||
```
|
||||
- 上面配置文件中用的是冒号,也可以用等号。
|
||||
|
||||
# 读取配置文件
|
||||
- 示例文件: example.py
|
||||
```python
|
||||
# -*- coding: utf-8 -*-
|
||||
import ConfigParser
|
||||
import string
|
||||
config=ConfigParser.ConfigParser()
|
||||
config.read(u'/proj/conf/example_conf')
|
||||
print string.upper(config.get("book","title")),
|
||||
print "by",config.get("book","author"),
|
||||
print "("+config.get("book","email")+")"
|
||||
print
|
||||
print config.get("size","size")
|
||||
print
|
||||
print config.sections()
|
||||
for section in config.sections():
|
||||
print section
|
||||
for option in config.options(section):
|
||||
print " ",option,"=",config.get(section,option)
|
||||
```
|
||||
- example.py文件执行结果
|
||||
```
|
||||
CONFIGPARSER模块教程 by 大头爸爸 (366500050@qq.com)
|
||||
1024
|
||||
['book', 'size', 'other']
|
||||
book
|
||||
title = ConfigParser模块教程
|
||||
author = 大头爸爸
|
||||
email = 366500050@qq.com
|
||||
time = 2012-09-20 22:04:55 size
|
||||
size = 1024 other
|
||||
blog = csdn.n
|
||||
```
|
||||
|
||||
# 写入配置文件
|
||||
- 示例
|
||||
```python
|
||||
import ConfigParser
|
||||
import sys
|
||||
config=ConfigParser.ConfigParser()
|
||||
config.add_section("book")
|
||||
config.set("book","title","这是标题")
|
||||
config.set("book","author","大头爸爸")
|
||||
config.add_section("size")
|
||||
config.set("size","size",1024)
|
||||
config.write(sys.stdout)
|
||||
```
|
||||
- 执行结果
|
||||
```
|
||||
[book]
|
||||
title = 这是标题
|
||||
author = 大头爸爸
|
||||
[size]
|
||||
size = 1024
|
||||
```
|
||||
|
||||
# ConfigParser方法
|
||||
- 创建ConfigParser实例
|
||||
```python
|
||||
config=ConfigParser.ConfigParser()
|
||||
```
|
||||
- 返回配置文件中节序列
|
||||
```python
|
||||
config.sections()
|
||||
```
|
||||
- 返回某个项目中的所有键的序列
|
||||
```python
|
||||
config=ConfigParser.ConfigParser()
|
||||
config.options(section)
|
||||
```
|
||||
- 返回section节中,option的键值
|
||||
```python
|
||||
config=ConfigParser.ConfigParser()
|
||||
config.get(section,option)
|
||||
```
|
||||
- 添加一个配置文件节点(str)
|
||||
```python
|
||||
config=ConfigParser.ConfigParser()
|
||||
config.add_section(str)
|
||||
```
|
||||
- 设置section节点中,键名为option的值(val)
|
||||
```python
|
||||
config=ConfigParser.ConfigParser()
|
||||
config.set(section,option,val)
|
||||
```
|
||||
- 读取配置文件
|
||||
```python
|
||||
config=ConfigParser.ConfigParser()
|
||||
config.read(filename)
|
||||
```
|
||||
- 写入配置文件
|
||||
```python
|
||||
config=ConfigParser.ConfigParser()
|
||||
config.write(obj_file)
|
||||
```
|
||||
|
||||
# 综合实例
|
||||
```python
|
||||
#coding=utf-8
|
||||
|
||||
import ConfigParser
|
||||
|
||||
def writeConfig(filename):
|
||||
config = ConfigParser.ConfigParser()
|
||||
# set db
|
||||
section_name = 'db'
|
||||
config.add_section( section_name )
|
||||
config.set( section_name, 'dbname', 'MySQL')
|
||||
config.set( section_name, 'host', '127.0.0.1')
|
||||
config.set( section_name, 'port', '80')
|
||||
config.set( section_name, 'password', '123456')
|
||||
config.set( section_name, 'databasename', 'test')
|
||||
|
||||
# set app
|
||||
section_name = 'app'
|
||||
config.add_section( section_name )
|
||||
config.set( section_name, 'loggerapp', '192.168.20.2')
|
||||
config.set( section_name, 'reportapp', '192.168.20.3')
|
||||
|
||||
# write to file
|
||||
config.write( open(filename, 'a') )
|
||||
|
||||
def updateConfig(filename, section, **keyv):
|
||||
config = ConfigParser.ConfigParser()
|
||||
config.read(filename)
|
||||
print config.sections()
|
||||
for section in config.sections():
|
||||
print "[",section,"]"
|
||||
items = config.items(section)
|
||||
for item in items:
|
||||
print "\t",item[0]," = ",item[1]
|
||||
print config.has_option("dbname", "MySQL")
|
||||
print config.set("db", "dbname", "11")
|
||||
print "..............."
|
||||
for key in keyv:
|
||||
print "\t",key," = ", keyv[key]
|
||||
config.write( open(filename, 'r+') )
|
||||
|
||||
if __name__ == '__main__':
|
||||
file_name = 'test.ini'
|
||||
writeConfig(file_name)
|
||||
updateConfig(file_name, 'app', reportapp = '192.168.100.100')
|
||||
print "end__"
|
||||
```
|
||||
|
Reference in New Issue
Block a user