在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在Python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是ConfigParser, Python ConfigParser模块解析的配置文件的格式比较象ini的配置文件格式

下面用实例说明如下:

配置文件db.conf 

[db]
db_host=10.1.10.15
db_port=3306
db_user=root
db_pass=59222999

连接数据程序如下:

#!/usr/bin/env python
# coding:utf8
#author: wangqiankun@lashou-inc.com import ConfigParser,string,os,sys
import MySQLdb dbconf = ConfigParser.ConfigParser()
dbconf.read("db.conf") #return section s = dbconf.sections()
print s,'\n'
o = dbconf.options('db')
print 'option:',o,'\n' v = dbconf.items("db")
print 'db:',v,'\n' #可以按照类型读取出来 db_host = dbconf.get("db","db_host")
db_port = dbconf.get("db",'db_port')
db_user = dbconf.get('db','db_user')
db_pass = dbconf.get('db','db_pass')
print db_host,db_port,db_user,db_pass, #connect
conn = MySQLdb.connect(host = db_host,user = db_user,passwd = db_pass, port = int(db_port),db = 'myapp')
cursor = conn.cursor() #select tables
t = cursor.execute('''show tables''')
for tr in cursor.fetchall():
print tr,
#select tables result
n = cursor.execute('select * from auth_user')
for rr in cursor.fetchall():
print rr, conn.close()

python 常用模块之ConfigParser的更多相关文章

  1. python常用模块之configparser模块

    python常用模块之configparser 作用:解析配置文件 假设在当前目录下有这样一个conf.ini文件 [DEFAULT] ServerAliveInterval = 45 Compres ...

  2. Python常用模块之configparser

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

  3. python常用模块-配置文档模块(configparser)

    python常用模块-配置文档模块(configparser) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. ConfigParser模块用于生成和修改常见配置文档,当前模块的名称 ...

  4. python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess logging re正则

    python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib  subprocess ...

  5. Python常用模块——目录

    Python常用模块学习 Python模块和包 Python常用模块time & datetime &random 模块 Python常用模块os & sys & sh ...

  6. python常用模块-调用系统命令模块(subprocess)

    python常用模块-调用系统命令模块(subprocess) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. subproces基本上就是为了取代os.system和os.spaw ...

  7. python常用模块及面向对象(一)

    目录: 常用模块之time模块 常用模块之random模块 常用模块之os模块 常用模块之sys模块 常用模块之subprocess模块 常用模块之json模块 常用模块之pickle模块 常用模块之 ...

  8. 常用模块(hashlib,configparser,logging)

    常用模块(hashlib,configparser,logging) hashlib hashlib 摘要算法的模块md5 sha1 sha256 sha512摘要的过程 不可逆能做的事:文件的一致性 ...

  9. python常用模块集合

    python常用模块集合 Python自定义模块 python collections模块/系列 Python 常用模块-json/pickle序列化/反序列化 python 常用模块os系统接口 p ...

随机推荐

  1. tensorboard入门

    Tensorboard tensorboard用以图形化展示我们的代码结构和图形化训练误差等,辅助优化程序 tensorboard实际上是tensorflow机器学习框架下的一个工具,需要先安装ten ...

  2. net项目调试时,读取主干或其他项目代码问题

    最近调试项目的时候出了一个很奇怪的问题. 项目总是去读取另外一个项目的配置文件,甚至执行的代码都是另外一个项目的. 我用vs修改代码时候,甚至修改到了其他项目的代码,生成不报错,很奇怪. 本来怀疑是v ...

  3. 蜗牛慢慢爬 LeetCode 8. String to Integer (atoi) [Difficulty: Medium]

    题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cas ...

  4. JavaScript设计模式学习之路——继承

    早在学习java的时候,就已经接触了继承,在java中因为有extends关键字,因此继承就比较简单.但是在JavaScript中,只能通过灵活的办法实现类的继承. 下面是我昨天在学习过程中,了解到的 ...

  5. Qt容器类汇总说明

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt容器类汇总说明     本文地址:http://techieliang.com/2017/ ...

  6. webgl学习笔记三-平移旋转缩放

    写在前面 建议先阅读下前面我的两篇文章. webgl学习笔记一-绘图单点 webgl学习笔记二-绘图多点 平移 1.关键点说明 顶点着色器需要加上 uniform vec4 u_Translation ...

  7. QT学习记录

    QApplication app(argc,argv); 创建了一个QApplication对象,这个对象用于管理应用程序级别的资源.QApplication的构造函数要求两个参数,分别来自main的 ...

  8. Spring boot整合shiro框架

    ShiroConfiguration package com.energy.common.config; import java.util.LinkedHashMap; import java.uti ...

  9. 第147天:web前端开发中的各种居中总结

    一.水平居中 方法① :行内元素 (父元素)text-align,(子元素)inline-block .parent{text-align: center;} .child{display: inli ...

  10. 第98天:CSS3中transform变换详解

    transform变换详解 本文主要介绍变形transform. Transform字面上就是变形,改变的意思.在CSS3中transform主要包括以下几种:旋转rotate.扭曲skew.缩放sc ...