configparser模块

echo   $@ $# $? $*

具体代码示例代码

import ConfigParser
import os class Config(object):
def __init__(self, config_filename="cgss.conf"):
print(config_filename)
file_path = file_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), config_filename) #注意这句的路径问题
self.cf = ConfigParser.ConfigParser()
self.cf.read(file_path)
print(self.get_sections()) def get_sections(self):
return self.cf.sections() def get_options(self, section):
return self.cf.options(section) def get_content(self, section):
result = {}
for option in self.get_options(section):
value = self.cf.get(section, option)
result[option] = int(value) if value.isdigit() else value
return result ret = Config().get_content("mongo")
print ret

cgss.cnf   
[notdbMysql]
host = 192.168.1.101
port = 3306
user = root
password = python123

详解

configparse用于处理特定格式的文件,其本质上利用open来操作文件(比如配置文件)
**********配置文件***************
#注释1这个一个配置文件

    [secton1] #节点
k1 = v1 #值
k2:v2 #值
[section2] #节点
k1 = v2#值

@1)、获取所有节点

import configparser
config = configparser.ConfigParser()
config.read('xxooo.txt', encoding='utf-8')
ret = config.sections()
print(ret)

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

    import configparser
config = configparser.ConfigParse()
config.read('xxoo.txt', encoding='utf-8')
ret = config.items('sections')
print(ret)

@3)、获取指定节点下所有的键

    import configparser
config = configparser.ConfigParser()
config.read("xxoo.txt", encoding="utf-8")
ret = config.options('section1')
print(ret)

@4)、获取指定节点下指定key值

    import configparser
config = configparser.ConfigParser()
config.read('xxoo.txt', encoding='utf-8')
v = config.get('section1', 'k1')
#v = config.getint('section1', 'k1')
#v = config.getfloat('section1', 'k1')
#v = config.getboolean('section1', 'k1')
print(v)

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

    import configparser
config = configparser.ConfigParser()
config.read('xxoo.txt', encoding='utf-8')
#检查
has_sec = config.has_section('section1')
print(has_sec)
#添加节点
config.add_section('SEC_1')
config.write(open('xxoo.txt', 'w'))
#删除节点
config.remove_section("SEC_1")
config.write(open("xxoo.txt", 'w'))

@6)、检查、删除、设置指定组内的键值对

import configparser
config = configparser.ConfigParser()
confgi.read('xxoo.txt', encoding='utf-8')
#检查
has_opt = config.has_option('section1','k1')
print(has_opt)
#删除
config.remove_option('section1', 'k1')
config.write(open('xxoo.txt','w'))
#设置
config.set('section1','k10','')
config.write(open("xxoo.txt",'w'))

最新文章

  1. 从零开始山寨Caffe·贰:主存模型
  2. 从表中删除重复记录的sql
  3. iPhone私有API
  4. SQL 数据类型,增删改查语句
  5. IOS 按比例裁剪图片
  6. 求给定数据中最小的K个数
  7. JavaScript引擎的工作原理
  8. Unicode-字符编码的历史由来(转)
  9. CentOS环境下R语言的安装和配置
  10. 在vhd中安装win7,并建立分差vhd
  11. 智能打印SDK-源码剖析
  12. git命令中带有特殊符号如@
  13. Android Stdio 如何自定义生成APK的名称
  14. Spring 学习——Spring AOP——AOP配置篇Advice(有参数传递)
  15. OpenVPN部署,实现访问云服务器的内网
  16. 码云git使用五(创建远程分支和更新远程分支)
  17. python中的面向对象学习之继承实例讲解
  18. 实例快速上手UDP和TCP的使用
  19. 分类器评估方法:精确度-召回率-F度量(precision-recall-F_measures)
  20. poj3190 Stall Reservations (贪心+优先队列)

热门文章

  1. 使用python实现appium的屏幕滑动
  2. iOS widget开发
  3. 在IIS中实现JSP
  4. 如何使用eclipse打开已有工程
  5. gradle项目与maven项目相互转化
  6. Query节点操作,jQuery插入节点,jQuery删除节点,jQuery Dom操作
  7. vue 配置文件详解
  8. Emgu.CV/opencv 绘图 线面文字包括中文
  9. Linux解压,压缩小总结
  10. thinkphp 3.2与phpexcel