﻿{"id":492,"date":"2011-05-27T17:22:25","date_gmt":"2011-05-27T09:22:25","guid":{"rendered":"http:\/\/nick.workao.org\/?p=492"},"modified":"2011-05-27T17:22:25","modified_gmt":"2011-05-27T09:22:25","slug":"python%e6%a8%a1%e5%9d%97%e4%b9%8bconfigparser-%e7%94%a8python%e8%a7%a3%e6%9e%90%e9%85%8d%e7%bd%ae%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/nick.txtcc.com\/index.php\/python\/492","title":{"rendered":"python\u6a21\u5757\u4e4bConfigParser: \u7528python\u89e3\u6790\u914d\u7f6e\u6587\u4ef6"},"content":{"rendered":"<p>python\u6a21\u5757\u4e4bConfigParser: \u7528python\u89e3\u6790\u914d\u7f6e\u6587\u4ef6\n\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u6765\u7075\u6d3b\u7684\u914d\u7f6e\u4e00\u4e9b\u53c2\u6570\u662f\u4e00\u4ef6\u5f88\u5e38\u89c1\u7684\u4e8b\u60c5\uff0c\u914d\u7f6e\u6587\u4ef6\u7684\u89e3\u6790\u5e76\u4e0d\u590d\u6742\uff0c\u5728python\u91cc\u66f4\u662f\u5982\u6b64\uff0c\u5728\u5b98\u65b9\u53d1\u5e03\u7684\u5e93\u4e2d\u5c31\u5305\u542b\u6709\u505a\u8fd9\u4ef6\u4e8b\u60c5\u7684\u5e93\uff0c\u90a3\u5c31\u662fConfigParser\uff0c\u8fd9\u91cc\u7b80\u5355\u7684\u505a\u4e00\u4e9b\u4ecb\u7ecd\u3002\nConfigParser\u89e3\u6790\u7684\u914d\u7f6e\u6587\u4ef6\u7684\u683c\u5f0f\u6bd4\u8f83\u8c61ini\u7684\u914d\u7f6e\u6587\u4ef6\u683c\u5f0f\uff0c\u5c31\u662f\u6587\u4ef6\u4e2d\u7531\u591a\u4e2asection\u6784\u6210\uff0c\u6bcf\u4e2asection\u4e0b\u53c8\u6709\u591a\u4e2a\u914d\u7f6e\u9879\uff0c\u6bd4\u5982\uff1a<\/p>\n<p>[db]\ndb_host=127.0.0.1\ndb_port=3306\ndb_user=root\ndb_pass=password\n[concurrent]\nthread=10\nprocessor=20<\/p>\n<p>\u5047\u8bbe\u4e0a\u9762\u7684\u914d\u7f6e\u6587\u4ef6\u7684\u540d\u5b57\u4e3atest.conf\u3002\u91cc\u9762\u5305\u542b\u4e24\u4e2asection,\u4e00\u4e2a\u662fdb, \u53e6\u4e00\u4e2a\u662fconcurrent, db\u91cc\u9762\u8fd8\u5305\u542b\u67094\u9879\uff0cconcurrent\u91cc\u9762\u6709\u4e24\u9879\u3002\u8fd9\u91cc\u6765\u505a\u505a\u89e3\u6790\uff1a<\/p>\n<p>#-<em>&#8211; encoding: gb2312 &#8211;<\/em>&#8211;\nimport ConfigParser\nimport string, os, sys<\/p>\n<p>cf = ConfigParser.ConfigParser()\ncf.read(&#8220;test.conf&#8221;)<\/p>\n<h1>\u8fd4\u56de\u6240\u6709\u7684section<\/h1>\n<p>s = cf.sections()\nprint &#8216;section:&#8217;, s<\/p>\n<p>o = cf.options(&#8220;db&#8221;)\nprint &#8216;options:&#8217;, o<\/p>\n<p>v = cf.items(&#8220;db&#8221;)\nprint &#8216;db:&#8217;, v<\/p>\n<p>print &#8216;-&#8216;*60\n#\u53ef\u4ee5\u6309\u7167\u7c7b\u578b\u8bfb\u53d6\u51fa\u6765\ndb_host = cf.get(&#8220;db&#8221;, &#8220;db_host&#8221;)\ndb_port = cf.getint(&#8220;db&#8221;, &#8220;db_port&#8221;)\ndb_user = cf.get(&#8220;db&#8221;, &#8220;db_user&#8221;)\ndb_pass = cf.get(&#8220;db&#8221;, &#8220;db_pass&#8221;)<\/p>\n<h1>\u8fd4\u56de\u7684\u662f\u6574\u578b\u7684<\/h1>\n<p>threads = cf.getint(&#8220;concurrent&#8221;, &#8220;thread&#8221;)\nprocessors = cf.getint(&#8220;concurrent&#8221;, &#8220;processor&#8221;)<\/p>\n<p>print &#8220;db_host:&#8221;, db_host\nprint &#8220;db_port:&#8221;, db_port\nprint &#8220;db_user:&#8221;, db_user\nprint &#8220;db_pass:&#8221;, db_pass<\/p>\n<p>print &#8220;thread:&#8221;, threads\nprint &#8220;processor:&#8221;, processors\n#\u4fee\u6539\u4e00\u4e2a\u503c\uff0c\u518d\u5199\u56de\u53bb\ncf.set(&#8220;db&#8221;, &#8220;db_pass&#8221;, &#8220;zhaowei&#8221;)\ncf.write(open(&#8220;test.conf&#8221;, &#8220;w&#8221;))<\/p>","protected":false},"excerpt":{"rendered":"<p>python\u6a21\u5757\u4e4bConfigParser: \u7528python\u89e3\u6790\u914d\u7f6e\u6587\u4ef6 \u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u6765\u7075\u6d3b\u7684\u914d\u7f6e\u4e00\u4e9b\u53c2\u6570\u662f\u4e00\u4ef6\u5f88\u5e38\u89c1\u7684\u4e8b\u60c5\uff0c\u914d\u7f6e\u6587\u4ef6\u7684\u89e3\u6790\u5e76\u4e0d\u590d\u6742\uff0c\u5728python\u91cc\u66f4\u662f\u5982\u6b64\uff0c\u5728\u5b98\u65b9\u53d1\u5e03\u7684\u5e93\u4e2d&#46;&#46;&#46;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[274],"tags":[288,275],"class_list":["post-492","post","type-post","status-publish","format-standard","hentry","category-python","tag-configparse","tag-python-2"],"_links":{"self":[{"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/492","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/comments?post=492"}],"version-history":[{"count":2,"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/492\/revisions"}],"predecessor-version":[{"id":494,"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/492\/revisions\/494"}],"wp:attachment":[{"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/media?parent=492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/categories?post=492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/tags?post=492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}