ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section), 每个节可以有多个参数(键=值)。使用的配置文件的好处就是不用在程序员写死,可以使程序更灵活。
注意:在python 3 中ConfigParser
模块名已更名为configparser
configparser函数常用方法:
读取配置文件:
1 read(filename) #读取配置文件,直接读取ini文件内容
2
3 sections() #获取ini文件内所有的section,以列表形式返回['logging', 'mysql']
4
5 options(sections) #获取指定sections下所有options ,以列表形式返回['host', 'port', 'user', 'password']
6
7 items(sections) #获取指定section下所有的键值对,[('host', '127.0.0.1'), ('port', '3306'), ('user', 'root'), ('password', '123456')]
8
9 get(section, option) #获取section中option的值,返回为string类型
10 >>>>>获取指定的section下的option <class 'str'> 127.0.0.1
11
12 getint(section,option) 返回int类型
13 getfloat(section, option) 返回float类型
14 getboolean(section,option) 返回boolen类型
举例如下:
配置文件ini如下:
[logging]
level = 20
path =
server =
[mysql]
host=127.0.0.1
port=3306
user=root
password=123456
注意,也可以使用:替换=
代码如下:
import configparser
from until.file_system import get_init_path
conf = configparser.ConfigParser()
file_path = get_init_path()
print(‘file_path :’,file_path)
conf.read(file_path)
sections = conf.sections()
print(‘获取配置文件所有的section’, sections)
options = conf.options(‘mysql’)
print(‘获取指定section下所有option’, options)
items = conf.items(‘mysql’)
print(‘获取指定section下所有的键值对’, items)
value = conf.get(‘mysql’, ‘host’)
print(‘获取指定的section下的option’, type(value), value)
运行结果如下:
file_path : /Users/xxx/Desktop/xxx/xxx/xxx.ini
获取配置文件所有的section ['logging', 'mysql']
获取指定section下所有option ['host', 'port', 'user', 'password']
获取指定section下所有的键值对 [('host', '127.0.0.1'), ('port', '3306'), ('user', 'root'), ('password', '123456')]
获取指定的section下的option <class 'str'> 127.0.0.1
综合使用方法:
import configparser
"""
读取配置文件信息
"""
class ConfigParser():
config_dic </span>=<span style="color: #000000;"> {}
@classmethod
</span><span style="color: #0000ff;">def</span><span style="color: #000000;"> get_config(cls, sector, item):
value </span>=<span style="color: #000000;"> None
</span><span style="color: #0000ff;">try</span><span style="color: #000000;">:
value </span>=<span style="color: #000000;"> cls.config_dic[sector][item]
</span><span style="color: #0000ff;">except</span><span style="color: #000000;"> KeyError:
cf </span>=<span style="color: #000000;"> configparser.ConfigParser()
cf.read(</span><span style="color: #800000;">'</span><span style="color: #800000;">settings.ini</span><span style="color: #800000;">'</span>, encoding=<span style="color: #800000;">'</span><span style="color: #800000;">utf8</span><span style="color: #800000;">'</span>) <span style="color: #008000;">#</span><span style="color: #008000;">注意setting.ini配置文件的路径</span>
value =<span style="color: #000000;"> cf.get(sector, item)
cls.config_dic </span>=<span style="color: #000000;"> value
</span><span style="color: #0000ff;">finally</span><span style="color: #000000;">:
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> value
if name == ‘main’:
con = ConfigParser()
res = con.get_config(‘logging’, ‘level’)
print(res)
参考链接:
http://blog.csdn.net/henulwj/article/details/49174355
后续补充~~~~
</div>
<div class="postDesc">posted @ <span id="post-date">2017-12-19 17:36</span> <a href="https://www.cnblogs.com/lhly/">修仙小白</a> 阅读(<span id="post_view_count">26421</span>) 评论(<span id="post_comment_count">0</span>) <a href="https://i.cnblogs.com/EditPosts.aspx?postid=8066898" rel="nofollow">编辑</a> <a href="#" onclick="AddToWz(8066898);return false;">收藏</a></div>
</div>
<script type="text/javascript">var allowComments=true,cb_blogId=353992,cb_entryId=8066898,cb_blogApp=currentBlogApp,cb_blogUserGuid='f7dde639-c932-e711-9fc1-ac853d9f53cc',cb_entryCreatedDate='2017/12/19 17:36:00';loadViewCount(cb_entryId);var cb_postType=1;var isMarkdown=false;</script>
</div><!--end: forFlow -->
</div><!--end: mainContent 主体内容容器-->
<div id="sideBar">
<div id="sideBarMain">
公告
<div id="blog-calendar" style=""><table id="blogCalendar" class="Cal" cellspacing="0" cellpadding="0" title="Calendar">
<tbody><tr><td colspan="7"><table class="CalTitle" cellspacing="0">
<tbody><tr><td class="CalNextPrev"><a href="javascript:void(0);" onclick="loadBlogCalendar('2019/05/01');return false;"><</a></td><td align="center">2019年6月</td><td class="CalNextPrev" align="right"><a href="javascript:void(0);" onclick="loadBlogCalendar('2019/07/01');return false;">></a></td></tr>
</tbody></table></td></tr><tr><th class="CalDayHeader" align="center" abbr="日" scope="col">日</th><th class="CalDayHeader" align="center" abbr="一" scope="col">一</th><th class="CalDayHeader" align="center" abbr="二" scope="col">二</th><th class="CalDayHeader" align="center" abbr="三" scope="col">三</th><th class="CalDayHeader" align="center" abbr="四" scope="col">四</th><th class="CalDayHeader" align="center" abbr="五" scope="col">五</th><th class="CalDayHeader" align="center" abbr="六" scope="col">六</th></tr><tr><td class="CalOtherMonthDay" align="center">26</td><td class="CalOtherMonthDay" align="center">27</td><td class="CalOtherMonthDay" align="center">28</td><td class="CalOtherMonthDay" align="center">29</td><td class="CalOtherMonthDay" align="center">30</td><td class="CalOtherMonthDay" align="center">31</td><td class="CalWeekendDay" align="center">1</td></tr><tr><td class="CalWeekendDay" align="center">2</td><td align="center">3</td><td align="center">4</td><td align="center">5</td><td align="center">6</td><td align="center">7</td><td class="CalWeekendDay" align="center">8</td></tr><tr><td class="CalWeekendDay" align="center">9</td><td align="center">10</td><td align="center">11</td><td align="center">12</td><td align="center">13</td><td align="center">14</td><td class="CalWeekendDay" align="center">15</td></tr><tr><td class="CalWeekendDay" align="center">16</td><td align="center">17</td><td align="center">18</td><td align="center">19</td><td class="CalTodayDay" align="center">20</td><td align="center">21</td><td class="CalWeekendDay" align="center">22</td></tr><tr><td class="CalWeekendDay" align="center">23</td><td align="center">24</td><td align="center">25</td><td align="center">26</td><td align="center">27</td><td align="center">28</td><td class="CalWeekendDay" align="center">29</td></tr><tr><td class="CalWeekendDay" align="center">30</td><td class="CalOtherMonthDay" align="center">1</td><td class="CalOtherMonthDay" align="center">2</td><td class="CalOtherMonthDay" align="center">3</td><td class="CalOtherMonthDay" align="center">4</td><td class="CalOtherMonthDay" align="center">5</td><td class="CalOtherMonthDay" align="center">6</td></tr>
<div id="leftcontentcontainer">
<div id="blog-sidecolumn"><div id="sidebar_search" class="sidebar-block">