# -*- coding: utf-8 -*-
#python 27
#xiaodeng
#python之模块ConfigParser(在python3中为configparser)
#特别注意:python3和python2关于该模块的功能用法有很大的不同. #配置文件解析器
import ConfigParser,os #初始化一个配置文件对象
config=ConfigParser.ConfigParser() #增加一个section
config.add_section('Section01')
config.add_section('Section02') #给section增加属性键值对
config.set('Section01','name','xiaodeng')#set(section, option, value)
config.set('Section01','age','')
config.set('Section01','bar','python')
config.set('Section01', 'an_int', '')
config.set('Section01', 'a_bool', 'true')
config.set('Section01', 'a_float', '3.1415')
config.set('Section01', 'baz', 'fun')
config.set('Section01', 'bar', 'fengMei')
config.set('Section01', 'foo', '%(bar)s is %(baz)s!')
config.set('Section01','age','')
config.set('Section02', 'bar', 'Python')
config.set('Section02','bar','python') #写入文件
with open('test.cfg','wb') as configFile:
config.write(configFile) #读取cfg文件
config=ConfigParser.ConfigParser()
config.read('test.cfg')#ConfigParser.ConfigParser instance #取值
print config.getfloat('Section01','a_float')#getfloat(section, options)
print config.getint('Section01','an_int')#getint(section, options)
print config.getboolean('Section01','a_bool')#getboolean(section, options) #返回一个list形式的元组;items(section, raw=False, vars=None)
print config.items('Section01')
#[('name', 'xiaodeng'), ('age', '25'), ('bar', 'fengMei'), ('an_int', '15'), ('a_bool', 'true'), ('a_float', '3.1415'), ('baz', 'fun'), ('foo', 'fengMei is fun!')] #取所有的section名字
print config.sections()#['Section01', 'Section02'] #获取所有的配置表名字key
print config.options('Section01')#['name', 'age', 'bar', 'an_int', 'a_bool', 'a_float', 'baz', 'foo'] #删除指定Section选项下内容
config.remove_section('Section02')#测试时查看文件中还是存在Section02,但是用指令查看sections的名字时只有Section01 #get(section, option, raw=False, vars=None)
#在指定的section选项中查找特定option的value值
#注意:这里默认查找的是最后一个相同option的值
print config.get('Section01','bar')
print config.get('Section01','age')
print config.get('Section01','bar') '''
ConfigParser -- responsible for parsing a list of
configuration files, and managing the parsed database. methods: __init__(defaults=None)
create the parser and specify a dictionary of intrinsic defaults. The
keys must be strings, the values must be appropriate for %()s string
interpolation. Note that `__name__' is always an intrinsic default;
its value is the section's name. sections()
#取所有的section名字
return all the configuration section names, sans DEFAULT has_section(section)
return whether the given section exists has_option(section, option)
return whether the given option exists in the given section options(section)
#获取所有的配置表名字key
return list of configuration options for the named section read(filenames)
read and parse the list of named configuration files, given by
name. A single filename is also allowed. Non-existing files
are ignored. Return list of successfully read files. readfp(fp, filename=None)
read and parse one configuration file, given as a file object.
The filename defaults to fp.name; it is only used in error
messages (if fp has no `name' attribute, the string `<???>' is used). get(section, option, raw=False, vars=None)
return a string value for the named option. All % interpolations are
expanded in the return values, based on the defaults passed into the
constructor and the DEFAULT section. Additional substitutions may be
provided using the `vars' argument, which must be a dictionary whose
contents override any pre-existing defaults. getint(section, options)
like get(), but convert value to an integer getfloat(section, options)
like get(), but convert value to a float getboolean(section, options)
like get(), but convert value to a boolean (currently case
insensitively defined as 0, false, no, off for False, and 1, true,
yes, on for True). Returns False or True. items(section, raw=False, vars=None)
return a list of tuples with (name, value) for each option
in the section. remove_section(section)
remove the given file section and all its options remove_option(section, option)
remove the given option from the given section set(section, option, value)
set the given option write(fp)
write the configuration state in .ini format add_section(self, section)
method of ConfigParser.ConfigParser instance Create a new section in the configuration.
'''

python之模块配置文件ConfigParser(在python3中变化较大)的更多相关文章

  1. Python 读取写入配置文件 —— ConfigParser

    Python 读取写入配置文件 —— ConfigParser Python 读取写入配置文件很方便,可使用内置的 configparser 模块:可查看源码,如博主本机地址: “C:/python2 ...

  2. python常用模块之configparser模块

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

  3. python xlrd 模块(获取Excel表中数据)

    python xlrd 模块(获取Excel表中数据) 一.安装xlrd模块   到python官网下载http://pypi.python.org/pypi/xlrd模块安装,前提是已经安装了pyt ...

  4. Python ymal 模块和configparser

    ymal : 是一种config文件 # !/user/bin/python # -*- coding: utf-8 -*- import configparser # 生成一个config文件 (当 ...

  5. Python 读取写入配置文件 ConfigParser

    https://blog.csdn.net/piaodexin/article/details/77371343 https://www.cnblogs.com/feeland/p/4502931.h ...

  6. python 常用模块之ConfigParser

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

  7. [tensorflow]图像处理相关模块的安装(python3中PIL)

    直接上过程图(平台为Anaconda): 默认已经配置完了tensorflow的3.5的环境 我这里已经安装完成 接下来,就可以在python文件中引入模块了 from PIL import Imag ...

  8. python之模块(在命令行当中使用pip install 模块来进行模块的安装)

    模块:在程序设计中,为完成某一功能所需的一段程序或子程序:或指能由编译程序.装配程序等处理的独立程序单位. 在我们编程的时候我们如果将所有的文件都放在那个py文件中,我们的py文件会很大,这样也很不好 ...

  9. Python常用模块之configparser

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

随机推荐

  1. [转]HIVE UDF/UDAF/UDTF的Map Reduce代码框架模板

    FROM : http://hugh-wangp.iteye.com/blog/1472371 自己写代码时候的利用到的模板   UDF步骤: 1.必须继承org.apache.hadoop.hive ...

  2. Windows Server 2012上安装.NET Framework 3.5(不需要安装光盘)

    因为在windows2012里,安装数据库,IIS部分组件都需要.NET3.5,而默认windows2012安装时,并不会把此组件复制到电脑里 导致,后期要安装.NET3.5还需要安装盘.但是,很多人 ...

  3. scala编程第16章学习笔记(3)——List类的高阶方法

    列表间映射:map.flatMap和foreach 1.xs map f 操作返回把函数f应用在xs的每个列表元素之后由此组成的新列表.如: scala> List(1, 2, 3) map ( ...

  4. arcgis server 10 for java 8399根目录是404的提示取消,并跳转到 地图目录 /arcgis/rest/services下

    看了Howto: 取消ArcGIS Server 9.x for Java内置tomcat在8399端口的文件列表 http://support.esrichina-bj.cn/2009/0819/9 ...

  5. Three.js中如何显示帧速【转】

    https://blog.csdn.net/hannahlwh1988/article/details/36876295 Step1:src中添加: <script src="js/s ...

  6. 关于微软C#中的CHART图表控件的简单使用【转】

    最近公司项目要用到Chart图表控件,这是一个比较老的东西了,目前网络上似乎已经不太流行这个控件,但是只要配置了相关的属性,效果还是可以的.前前后后摸索了好久,接下来谈谈这个件控件最重要的几个属性. ...

  7. JQuery-Dialog(弹出窗口,遮蔽窗口)

    在Ajax中经常用到的弹出窗口和遮蔽窗口.自己写肯定是一个最佳方案,但时间和成本上,还是决定了寻找现成的吧.大概罗列一下.需要我满足我几个条件 一定要简洁方便 拥有遮蔽功能,Model Dialog ...

  8. VB.NET,C#.NET调用Web Service,利用visual studio 的实现方法

    下面是一篇文章比较详细,其实具体操作很简单,把Web Service服务地址,利用工具(VS2010),通过添加引用的形式,添加到项目中来就可以应用了. 大家如果这个地方不会操场的话,可以问问我QQ: ...

  9. 【Scala】Scala多线程-并发实践

    Scala多线程-并发实践 scala extends Thread_百度搜索 scala多线程 - 且穷且独立 - 博客园 Scala和并发编程 - Andy Tech Talk - ITeye博客 ...

  10. sklearn文本特征提取

    http://cloga.info/2014/01/19/sklearn_text_feature_extraction/ 文本特征提取 词袋(Bag of Words)表征 文本分析是机器学习算法的 ...