{"id":201,"date":"2023-02-22T22:18:58","date_gmt":"2023-02-22T14:18:58","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=201"},"modified":"2023-04-30T15:29:55","modified_gmt":"2023-04-30T07:29:55","slug":"python-use-configparser-to-read-and-write-ini-configuration-files","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/22\/python-use-configparser-to-read-and-write-ini-configuration-files\/","title":{"rendered":"Python\u4f7f\u7528ConfigParser\u8bfb\u5199ini\u914d\u7f6e\u6587\u4ef6"},"content":{"rendered":"<h4>\u914d\u7f6e\u6587\u4ef6\uff1aconfig.ini<\/h4>\n<pre><code>[global]\ncountry = China\nlanguage = zh-CN\n\n[custom]\nhome = appblog.cn<\/code><\/pre>\n<p><!-- more --><\/p>\n<h4>Python\u811a\u672c<\/h4>\n<pre><code class=\"language-python\"># -*- coding: utf-8 -*-\nimport ConfigParser\nimport os\n\ndef set_ConfigParser(filename, section, option, value):\n    # \u5199\u914d\u7f6e\n    if os.path.exists(filename):\n        conf = ConfigParser.ConfigParser()\n        conf.read(filename)\n        set_value = conf.set(section, option, value)\n        conf.write(open(filename, &quot;w+&quot;))\n    else:\n        set_value = &#039;&#039;\n    return set_value\n\ndef get_ConfigParser(filename, section, option):\n    # \u8bfb\u914d\u7f6e\n    if os.path.exists(filename):\n        conf = ConfigParser.ConfigParser()\n        conf.read(filename)\n        get_value = conf.get(section, option)\n    else:\n        get_value = &#039;&#039;\n    return get_value\n\nif __name__ == &quot;__main__&quot;:\n    set_ConfigParser(&#039;config.ini&#039;, &#039;custom&#039;, &#039;name&#039;, &#039;Joe.Ye&#039;)\n    print get_ConfigParser(&#039;config.ini&#039;, &#039;custom&#039;, &#039;name&#039;)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u914d\u7f6e\u6587\u4ef6\uff1aconfig.ini [global] country = China language = zh- [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-201","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/201","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=201"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/201\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}