ConfigParser-- 读取写入配置文件
基础读取配置文件
- -read(filename) 直接读取文件内容
- -sections() 得到所有的section,并以列表的形式返回
- -options(section) 得到该section的所有option
- -items(section) 得到该section的所有键值对
- -get(section,option) 得到section中option的值,返回为string类型
- -getint(section,option) 得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat() 函数。
import ConfigParser,os
os.chdir('C:\\Users\\hito\\Desktop')
cf = ConfigParser.ConfigParser()
cf.read('test.ini')
a=cf.get('section1','option1')
print a+'a'
b=cf.getint('section1','option1')
print a+2
基础写入配置文件
- -write(fp) 将config对象写入至某个 .init 格式的文件
- -add_section(section) 添加一个新的section
- -set( section, option, value 对section中的option进行设置,需要调用write将内容写入配置文件
- -remove_section(section) 删除某个 section
- -remove_option(section, option) 删除某个 section 下的 option
import ConfigParser,os
os.chdir('C:\\Users\\hito\\Desktop')
cf = ConfigParser.ConfigParser()
cf.add_section("section1")
cf.set("section1", "option1", 1)
cf.add_section("section2")
cf.set("section2", "option2", "value2")
# write to file
with open("test.ini","w+") as f:
cf.write(f)
ConfigParser-- 读取写入配置文件的更多相关文章
- Python 读取写入配置文件 —— ConfigParser
Python 读取写入配置文件 —— ConfigParser Python 读取写入配置文件很方便,可使用内置的 configparser 模块:可查看源码,如博主本机地址: “C:/python2 ...
- 【python3】configparser读取ini配置文件
在应用过程中,发现下面这个问题: cf=configparser.ConfigParser()读取配置文件时,如果数据包含%这们析特殊符号,就会报出上面的错误,使用cf = configparser. ...
- Python 读取写入配置文件 ConfigParser
https://blog.csdn.net/piaodexin/article/details/77371343 https://www.cnblogs.com/feeland/p/4502931.h ...
- python的ConfigParser读取设置配置文件
python 读写配置文件在实际应用中具有十分强大的功能,在实际的操作中也有相当简捷的操作方案,以下的文章就是对python 读写配置文件的具体方案的介绍,望你浏览完下面的文章会有所收获. pytho ...
- Python使用ConfigParser模块读取配置文件(config.ini)以及写入配置文件
前言 使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是configParser.configPars ...
- python3使用configparser读取配置文件
python2中的ConfigParser在python3中改成了configparser 1.配置文件格式是 [域名] k=v 2.代码示例:需要生成conf.ini配置文件如下:[config]v ...
- python ConfigParser读取配置文件,及解决报错(去掉BOM)ConfigParser.MissingSectionHeaderError: File contains no section headers的方法
先说一下在读取配置文件时报错的问题--ConfigParser.MissingSectionHeaderError: File contains no section headers 问题描述: 在练 ...
- 记一次用python 的ConfigParser读取配置文件编码报错
记一次用python 的ConfigParser读取配置文件编码报错 ...... raise MissingSectionHeaderError(fpname, lineno, line)Confi ...
- 用ConfigParser模块读写配置文件——Python
对于功能较多.考虑用户体验的程序,配置功能是必不可少的,如何存储程序的各种配置? 1)可以用全局变量,不过全局变量具有易失性,程序崩溃或者关闭之后配置就没了,再者配置太多,将变量分配到哪里也是需要考虑 ...
随机推荐
- Bootstrap入门(十三)组件7:导航条
Bootstrap入门(十三)组件7:导航条 1.默认样式的导航条 2.嵌入表单和按钮 3.嵌入文本和非导航的链接 4.组件排列和下拉菜单 5.固定在顶部/底部 6.反色的导航条 7.路径导航 首先先 ...
- Unable to find setter method for attribute: 属性名
tld文件里面的名称和标签内的属性名要一致.
- python 的日志相关应用
python日志主要用logging模块; 示例代码如下: #coding:utf-8 import logging class logger(): ''' %(asctime)s %(filenam ...
- Codeforces758B
B. Blown Garland time limit per test:1 second memory limit per test:256 megabytes input:standard inp ...
- Javaweb程序服务器部署
话说从接触web后就想着写一个自己的站点然后别人都可以访问,这也是一个小目标吧,从之前在使用校园网的时候把自己的电脑当成服务器然后部署使用同学的电脑访问,现在想让所有人都可以访问,于是就花重金租了腾讯 ...
- ubuntu 14.04 中找不到 libgtk-x11-2.0.so
如果ubuntu安装的64位的,在其中安装32位软件时就会碰到缺失libgtk-x11-2.0.so的情况 比如用wine安装qq时 启动qq时就会报这样的错误 error : cannot open ...
- Linux Platform驱动模型(一)-设备信息
我在Linux字符设备驱动框架一文中简单介绍了Linux字符设备编程模型,在那个模型中,只要应用程序open()了相应的设备文件,就可以使用ioctl通过驱动程序来控制我们的硬件,这种模型直观,但是从 ...
- 自学javaee程序员之路--ssm的小项目(一)
大家好~我叫王聪,缩写是WC(不是厕所!不是厕所!).是一名某内陆大四的学生.这两个月自学了javaee---关于web的一些心得,分享记录一下.建立这个博客的目的是望各位前辈学长指正批评~~也是建立 ...
- vs生成配置release自动恢复成debug无法配置的问题
软件为visual studio 2015,右键单个项目选属性,设置“生成配置”release,保存后,再次右键属性,却发现又自动恢复成debug,怎么回事?是无法配置吗?点击生成试试,果然2b了. ...
- 通过web sql实现增删查改
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...