python-ConfigParser模块【读写配置文件】
对python 读写配置文件的具体方案的介绍
1,函数介绍
1 |
import configParser 如果Configparser无效将导入的configParser 的C小写
1 |
1.1.读取配置文件
-read(filename) 直接读取ini文件内容
-sections() 得到所有的section,并以列表的形式返回
-options(section) 得到该section的所有option
-items(section) 得到该section的所有键值对
-get(section,option) 得到section中option的值,返回为string类型
-getint(section,option) 得到section中option的值,返回为int类型
1.2.写入配置文件
-add_section(section) 添加一个新的section
-set( section, option, value) 对section中的option进行设置
需要调用write将内容写入配置文件。
2,测试实例
2.1,测试1
配置文件test.cfg
1 2 3 4 5 6 7 8 9 | [sec_a] a_key1 = 20 a_key2 = 10 [sec_b] b_key1 = 121 b_key2 = b_value2 b_key3 = $r b_key4 = 127.0 . 0.1 |
测试文件test.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | # -* - coding: UTF-8 -* - import configParser #生成config对象 conf = configParser.ConfigParser() #用config对象读取配置文件 conf.read( "test.cfg" ) #以列表形式返回所有的section sections = conf.sections() print 'sections:' , sections #sections: ['sec_b', 'sec_a'] #得到指定section的所有option options = conf.options( "sec_a" ) print 'options:' , options #options: ['a_key1', 'a_key2'] #得到指定section的所有键值对 kvs = conf.items( "sec_a" ) print 'sec_a:' , kvs #sec_a: [('a_key1', '20'), ('a_key2', '10')] #指定section,option读取值 str_val = conf.get( "sec_a" , "a_key1" ) int_val = conf.getint( "sec_a" , "a_key2" ) print "value for sec_a's a_key1:" , str_val #value for sec_a's a_key1: 20 print "value for sec_a's a_key2:" , int_val #value for sec_a's a_key2: 10 #写配置文件 #更新指定section,option的值 conf. set ( "sec_b" , "b_key3" , "new-$r" ) #写入指定section增加新option和值 conf. set ( "sec_b" , "b_newkey" , "new-value" ) #增加新的section conf.add_section( 'a_new_section' ) conf. set ( 'a_new_section' , 'new_key' , 'new_value' ) #写回配置文件 conf.write( open ( "test.cfg" , "w" )) |
2.2,测试2
1 2 3 4 | [info] age = 21 name = chen sex = male |
测试文件test.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | from __future__ import with_statement import configParser config = ConfigParser.ConfigParser() with open ( "test.cfg" , "rw" ) as cfgfile: config.readfp(cfgfile) name = config.get( "info" , "name" ) age = config.get( "info" , "age" ) print name print age config. set ( "info" , "sex" , "male" ) config. set ( "info" , "age" , "55" ) age = config.getint( "info" , "age" ) print name print type (age) print age |
分析
其中[ ] 中的info是这段配置的名字。
其中age,name都是属性。
首先,config=ConfigParser.ConfigParser() 得到一个配置config对象.下面打开一个配置文件 cfgfile. 用readfp()读取这个文件.这样配置的内容就读到config对象里面了。
接下来一个问题是如何读取值.常用的方法是get() 和getint() . get()返回文本. getint()返回整数。
其次,name=config.get(''info'',''name'') 意思就是.读取config中info段中的name变量值。
最后讲讲如何设置值.使用set(段名,变量名,值) 来设置变量.config.set(''info'',''age'',''21'') 表示把info段中age变量设置为21。
2.3,测试3
Python的ConfigParser Module中定义了3个类对INI文件进行操作。
分别是RawConfigParser、ConfigParser、SafeConfigParser。
RawCnfigParser是最基础的INI文件读取类,ConfigParser、SafeConfigParser支持对%(value)s变量的解析。
配置文件test.cfg
1 2 3 4 | [portal] url = http: / / % (host)s: % (port)s / Portal host = localhost port = 8080 |
使用RawConfigParser:
1 2 3 4 5 6 7 8 9 10 | import ConfigParser conf = ConfigParser.RawConfigParser() print "use RawConfigParser() read" conf.read( "test.cfg" ) print conf.get( "portal" , "url" ) print "use RawConfigParser() write" conf. set ( "portal" , "url2" , "%(host)s:%(port)s" ) print conf.get( "portal" , "url2" ) |
得到输出
1 2 3 4 | use RawConfigParser() read http: / / % (host)s: % (port)s / Portal use RawConfigParser() write % (host)s: % (port)s |
改用ConfigParser
1 2 3 4 5 6 7 8 9 10 | import ConfigParser conf = ConfigParser.ConfigParser() print "use RawConfigParser() read" conf.read( "test.cfg" ) print conf.get( "portal" , "url" ) print "use RawConfigParser() write" conf. set ( "portal" , "url2" , "%(host)s:%(port)s" ) print conf.get( "portal" , "url2" ) |
得到输出
1 2 3 4 | use RawConfigParser() read http: / / localhost: 8080 / Portal use RawConfigParser() write localhost: 8080 |
改用SafeConfigParser,效果与ConfigParser相同
1 2 3 4 5 6 7 8 9 10 | import ConfigParser conf = ConfigParser.SafeConfigParser() print "use RawConfigParser() read" conf.read( "test.cfg" ) print conf.get( "portal" , "url" ) print "use RawConfigParser() write" conf. set ( "portal" , "url2" , "%(host)s:%(port)s" ) print conf.get( "portal" , "url2" ) |
结论:
还是用ConfigParser
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
2016-09-12 jQuery判断checkbox是否选中的3种方法