{"id":1162420,"date":"2025-01-15T14:43:07","date_gmt":"2025-01-15T06:43:07","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1162420.html"},"modified":"2025-01-15T14:43:10","modified_gmt":"2025-01-15T06:43:10","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%86%99conf","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1162420.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5199conf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25203448\/d50e5b55-be5e-46ca-9c50-15437042e829.webp\" alt=\"python\u4e2d\u5982\u4f55\u5199conf\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u7f16\u5199\u914d\u7f6e\u6587\u4ef6\uff08conf\uff09\u901a\u5e38\u6709\u591a\u79cd\u65b9\u5f0f\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u3001\u73af\u5883\u53d8\u91cf\u3001\u547d\u4ee4\u884c\u53c2\u6570\u7b49\u3002<\/strong> <strong>\u5176\u4e2d\uff0c\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u662f\u6700\u4e3a\u5e38\u89c1\u548c\u63a8\u8350\u7684\u65b9\u5f0f\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u7ba1\u7406\u5927\u91cf\u914d\u7f6e\u53c2\u6570\u65f6\u3002\u5e38\u89c1\u7684\u914d\u7f6e\u6587\u4ef6\u683c\u5f0f\u6709INI\u3001YAML\u3001JSON\u7b49\u3002<\/strong> <strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528INI\u3001YAML\u548cJSON\u683c\u5f0f\u7684\u914d\u7f6e\u6587\u4ef6\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001INI \u914d\u7f6e\u6587\u4ef6<\/h3>\n<\/p>\n<p><h4>1\u3001\u4ec0\u4e48\u662fINI\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>INI\u6587\u4ef6\u662f\u4e00\u79cd\u7b80\u5355\u7684\u3001\u6587\u672c\u683c\u5f0f\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u901a\u5e38\u7531\u8282\uff08section\uff09\u3001\u952e\uff08key\uff09\u548c\u503c\uff08value\uff09\u7ec4\u6210\u3002\u5b83\u7684\u7ed3\u6784\u975e\u5e38\u6e05\u6670\uff0c\u6613\u4e8e\u9605\u8bfb\u548c\u7f16\u5199\u3002INI\u6587\u4ef6\u901a\u5e38\u5177\u6709\u4ee5\u4e0b\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-ini\">[section1]<\/p>\n<p>key1 = value1<\/p>\n<p>key2 = value2<\/p>\n<p>[section2]<\/p>\n<p>key3 = value3<\/p>\n<p>key4 = value4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bfb\u53d6INI\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>configparser<\/code>\u6a21\u5757\u6765\u8bfb\u53d6\u548c\u5199\u5165INI\u683c\u5f0f\u7684\u914d\u7f6e\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import configparser<\/p>\n<h2><strong>\u521b\u5efaConfigParser\u5bf9\u8c61<\/strong><\/h2>\n<p>config = configparser.ConfigParser()<\/p>\n<h2><strong>\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6<\/strong><\/h2>\n<p>config.read(&#39;config.ini&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u6307\u5b9asection\u548ckey\u7684\u503c<\/strong><\/h2>\n<p>value1 = config.get(&#39;section1&#39;, &#39;key1&#39;)<\/p>\n<p>value2 = config.getint(&#39;section1&#39;, &#39;key2&#39;)  # \u5982\u679c\u503c\u662f\u6574\u6570<\/p>\n<p>value3 = config.getboolean(&#39;section2&#39;, &#39;key3&#39;)  # \u5982\u679c\u503c\u662f\u5e03\u5c14\u7c7b\u578b<\/p>\n<p>print(value1, value2, value3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5199\u5165INI\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import configparser<\/p>\n<h2><strong>\u521b\u5efaConfigParser\u5bf9\u8c61<\/strong><\/h2>\n<p>config = configparser.ConfigParser()<\/p>\n<h2><strong>\u6dfb\u52a0section\u548ckey-value\u5bf9<\/strong><\/h2>\n<p>config[&#39;section1&#39;] = {<\/p>\n<p>    &#39;key1&#39;: &#39;value1&#39;,<\/p>\n<p>    &#39;key2&#39;: &#39;value2&#39;<\/p>\n<p>}<\/p>\n<p>config[&#39;section2&#39;] = {<\/p>\n<p>    &#39;key3&#39;: &#39;value3&#39;,<\/p>\n<p>    &#39;key4&#39;: &#39;value4&#39;<\/p>\n<p>}<\/p>\n<h2><strong>\u5199\u5165\u914d\u7f6e\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;config.ini&#39;, &#39;w&#39;) as configfile:<\/p>\n<p>    config.write(configfile)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001YAML \u914d\u7f6e\u6587\u4ef6<\/h3>\n<\/p>\n<p><h4>1\u3001\u4ec0\u4e48\u662fYAML\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>YAML\uff08YAML <a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n&#39;t Markup Language\uff09\u662f\u4e00\u79cd\u6bd4JSON\u548cXML\u66f4\u7b80\u6d01\u7684\u914d\u7f6e\u6587\u4ef6\u683c\u5f0f\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5404\u7c7b\u5e94\u7528\u7a0b\u5e8f\u4e2d\u3002YAML\u7684\u8bed\u6cd5\u7b80\u5355\u6613\u61c2\uff0c\u652f\u6301\u5d4c\u5957\u7ed3\u6784\uff0c\u975e\u5e38\u9002\u5408\u7528\u6765\u7f16\u5199\u914d\u7f6e\u6587\u4ef6\u3002YAML\u6587\u4ef6\u901a\u5e38\u5177\u6709\u4ee5\u4e0b\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-yaml\">section1:<\/p>\n<p>  key1: value1<\/p>\n<p>  key2: value2<\/p>\n<p>section2:<\/p>\n<p>  key3: value3<\/p>\n<p>  key4: value4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bfb\u53d6YAML\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pyyaml<\/code>\u5e93\u6765\u8bfb\u53d6\u548c\u5199\u5165YAML\u683c\u5f0f\u7684\u914d\u7f6e\u6587\u4ef6\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5\u8be5\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyyaml<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u8bfb\u53d6YAML\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import yaml<\/p>\n<h2><strong>\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;config.yaml&#39;, &#39;r&#39;) as file:<\/p>\n<p>    config = yaml.<a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_load(file)<\/p>\n<h2><strong>\u83b7\u53d6\u6307\u5b9asection\u548ckey\u7684\u503c<\/strong><\/h2>\n<p>value1 = config[&#39;section1&#39;][&#39;key1&#39;]<\/p>\n<p>value2 = config[&#39;section1&#39;][&#39;key2&#39;]<\/p>\n<p>value3 = config[&#39;section2&#39;][&#39;key3&#39;]<\/p>\n<p>print(value1, value2, value3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5199\u5165YAML\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import yaml<\/p>\n<h2><strong>\u5b9a\u4e49\u914d\u7f6e\u6570\u636e<\/strong><\/h2>\n<p>config = {<\/p>\n<p>    &#39;section1&#39;: {<\/p>\n<p>        &#39;key1&#39;: &#39;value1&#39;,<\/p>\n<p>        &#39;key2&#39;: &#39;value2&#39;<\/p>\n<p>    },<\/p>\n<p>    &#39;section2&#39;: {<\/p>\n<p>        &#39;key3&#39;: &#39;value3&#39;,<\/p>\n<p>        &#39;key4&#39;: &#39;value4&#39;<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<h2><strong>\u5199\u5165\u914d\u7f6e\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;config.yaml&#39;, &#39;w&#39;) as file:<\/p>\n<p>    yaml.dump(config, file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001JSON \u914d\u7f6e\u6587\u4ef6<\/h3>\n<\/p>\n<p><h4>1\u3001\u4ec0\u4e48\u662fJSON\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>JSON\uff08JavaScript Object Notation\uff09\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u6613\u4e8e\u4eba\u9605\u8bfb\u548c\u7f16\u5199\uff0c\u540c\u65f6\u4e5f\u6613\u4e8e\u673a\u5668\u89e3\u6790\u548c\u751f\u6210\u3002JSON\u683c\u5f0f\u5728Python\u4e2d\u4e5f\u88ab\u5e7f\u6cdb\u4f7f\u7528\uff0c\u901a\u5e38\u5177\u6709\u4ee5\u4e0b\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-json\">{<\/p>\n<p>  &quot;section1&quot;: {<\/p>\n<p>    &quot;key1&quot;: &quot;value1&quot;,<\/p>\n<p>    &quot;key2&quot;: &quot;value2&quot;<\/p>\n<p>  },<\/p>\n<p>  &quot;section2&quot;: {<\/p>\n<p>    &quot;key3&quot;: &quot;value3&quot;,<\/p>\n<p>    &quot;key4&quot;: &quot;value4&quot;<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bfb\u53d6JSON\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>json<\/code>\u6a21\u5757\u6765\u8bfb\u53d6\u548c\u5199\u5165JSON\u683c\u5f0f\u7684\u914d\u7f6e\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;config.json&#39;, &#39;r&#39;) as file:<\/p>\n<p>    config = json.load(file)<\/p>\n<h2><strong>\u83b7\u53d6\u6307\u5b9asection\u548ckey\u7684\u503c<\/strong><\/h2>\n<p>value1 = config[&#39;section1&#39;][&#39;key1&#39;]<\/p>\n<p>value2 = config[&#39;section1&#39;][&#39;key2&#39;]<\/p>\n<p>value3 = config[&#39;section2&#39;][&#39;key3&#39;]<\/p>\n<p>print(value1, value2, value3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5199\u5165JSON\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u5b9a\u4e49\u914d\u7f6e\u6570\u636e<\/strong><\/h2>\n<p>config = {<\/p>\n<p>    &#39;section1&#39;: {<\/p>\n<p>        &#39;key1&#39;: &#39;value1&#39;,<\/p>\n<p>        &#39;key2&#39;: &#39;value2&#39;<\/p>\n<p>    },<\/p>\n<p>    &#39;section2&#39;: {<\/p>\n<p>        &#39;key3&#39;: &#39;value3&#39;,<\/p>\n<p>        &#39;key4&#39;: &#39;value4&#39;<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<h2><strong>\u5199\u5165\u914d\u7f6e\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;config.json&#39;, &#39;w&#39;) as file:<\/p>\n<p>    json.dump(config, file, indent=4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u73af\u5883\u53d8\u91cf<\/h3>\n<\/p>\n<p><h4>1\u3001\u4ec0\u4e48\u662f\u73af\u5883\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u73af\u5883\u53d8\u91cf\u662f\u4e00\u79cd\u5728\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u52a8\u6001\u547d\u540d\u7684\u503c\uff0c\u53ef\u4ee5\u88ab\u591a\u4e2a\u5e94\u7528\u7a0b\u5e8f\u8bbf\u95ee\u3002\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u6765\u914d\u7f6e\u5e94\u7528\u7a0b\u5e8f\u5177\u6709\u4ee5\u4e0b\u4f18\u70b9\uff1a\u5b89\u5168\u6027\u9ad8\u3001\u6613\u4e8e\u7ba1\u7406\u3001\u9002\u7528\u4e8e\u5bb9\u5668\u5316\u73af\u5883\uff08\u5982Docker\uff09\u7b49\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u8bfb\u53d6\u73af\u5883\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u6765\u8bfb\u53d6\u73af\u5883\u53d8\u91cf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u8bfb\u53d6\u73af\u5883\u53d8\u91cf<\/strong><\/h2>\n<p>value1 = os.getenv(&#39;KEY1&#39;)<\/p>\n<p>value2 = os.getenv(&#39;KEY2&#39;)<\/p>\n<p>print(value1, value2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u7684\u65b9\u6cd5\u56e0\u64cd\u4f5c\u7cfb\u7edf\u800c\u5f02\u3002\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">set KEY1=value1<\/p>\n<p>set KEY2=value2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Unix\/Linux\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">export KEY1=value1<\/p>\n<p>export KEY2=value2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u547d\u4ee4\u884c\u53c2\u6570<\/h3>\n<\/p>\n<p><h4>1\u3001\u4ec0\u4e48\u662f\u547d\u4ee4\u884c\u53c2\u6570<\/h4>\n<\/p>\n<p><p>\u547d\u4ee4\u884c\u53c2\u6570\u662f\u4e00\u79cd\u901a\u8fc7\u547d\u4ee4\u884c\u4f20\u9012\u7ed9\u7a0b\u5e8f\u7684\u53c2\u6570\uff0c\u901a\u5e38\u7528\u4e8e\u914d\u7f6e\u548c\u63a7\u5236\u7a0b\u5e8f\u7684\u884c\u4e3a\u3002\u547d\u4ee4\u884c\u53c2\u6570\u7684\u4f18\u70b9\u662f\u7075\u6d3b\u6027\u9ad8\uff0c\u9002\u7528\u4e8e\u5feb\u901f\u6d4b\u8bd5\u548c\u8c03\u8bd5\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u8bfb\u53d6\u547d\u4ee4\u884c\u53c2\u6570<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>argparse<\/code>\u6a21\u5757\u6765\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import argparse<\/p>\n<h2><strong>\u521b\u5efaArgumentParser\u5bf9\u8c61<\/strong><\/h2>\n<p>parser = argparse.ArgumentParser(description=&#39;Example script with command line arguments&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u547d\u4ee4\u884c\u53c2\u6570<\/strong><\/h2>\n<p>parser.add_argument(&#39;--key1&#39;, type=str, help=&#39;Description for key1&#39;)<\/p>\n<p>parser.add_argument(&#39;--key2&#39;, type=int, help=&#39;Description for key2&#39;)<\/p>\n<h2><strong>\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570<\/strong><\/h2>\n<p>args = parser.parse_args()<\/p>\n<h2><strong>\u83b7\u53d6\u53c2\u6570\u503c<\/strong><\/h2>\n<p>value1 = args.key1<\/p>\n<p>value2 = args.key2<\/p>\n<p>print(value1, value2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u7f16\u5199\u914d\u7f6e\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\u3002INI\u3001YAML\u548cJSON\u6587\u4ef6\u683c\u5f0f\u9002\u7528\u4e8e\u9700\u8981\u7ba1\u7406\u5927\u91cf\u914d\u7f6e\u53c2\u6570\u7684\u60c5\u51b5\uff0c\u73af\u5883\u53d8\u91cf\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u5b89\u5168\u6027\u548c\u6613\u4e8e\u7ba1\u7406\u7684\u60c5\u51b5\uff0c\u547d\u4ee4\u884c\u53c2\u6570\u9002\u7528\u4e8e\u5feb\u901f\u6d4b\u8bd5\u548c\u8c03\u8bd5\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u751a\u81f3\u53ef\u4ee5\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u66f4\u7075\u6d3b\u7684\u914d\u7f6e\u7ba1\u7406\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u5e94\u8be5\u786e\u4fdd\u914d\u7f6e\u6587\u4ef6\u7684\u7ed3\u6784\u6e05\u6670\u3001\u6613\u4e8e\u9605\u8bfb\u548c\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u914d\u7f6e\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u683c\u5f0f\u6765\u521b\u5efa\u914d\u7f6e\u6587\u4ef6\uff0c\u4f8b\u5982INI\u3001JSON\u3001YAML\u7b49\u3002\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u662f\u4f7f\u7528<code>configparser<\/code>\u6a21\u5757\u6765\u5904\u7406INI\u683c\u5f0f\u7684\u914d\u7f6e\u6587\u4ef6\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5bfc\u5165\u6a21\u5757\u5e76\u521b\u5efa\u4e00\u4e2a\u914d\u7f6e\u5bf9\u8c61\uff0c\u7136\u540e\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u8282\u548c\u952e\u6765\u5199\u5165\u914d\u7f6e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import configparser\n\nconfig = configparser.ConfigParser()\nconfig[&#39;DEFAULT&#39;] = {&#39;Server&#39;: &#39;localhost&#39;,\n                     &#39;Port&#39;: &#39;8080&#39;}\nconfig[&#39;DATABASE&#39;] = {&#39;User&#39;: &#39;admin&#39;, &#39;Password&#39;: &#39;secret&#39;}\n\nwith open(&#39;config.ini&#39;, &#39;w&#39;) as configfile:\n    config.write(configfile)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u5c31\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u540d\u4e3a<code>config.ini<\/code>\u7684\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u540c\u6837\u7b80\u5355\u3002\u4f7f\u7528<code>configparser<\/code>\u6a21\u5757\u53ef\u4ee5\u8f7b\u677e\u63d0\u53d6\u5b58\u50a8\u5728INI\u683c\u5f0f\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u3002\u53ea\u9700\u521b\u5efa\u4e00\u4e2a\u914d\u7f6e\u5bf9\u8c61\u5e76\u8c03\u7528<code>read<\/code>\u65b9\u6cd5\u6765\u52a0\u8f7d\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import configparser\n\nconfig = configparser.ConfigParser()\nconfig.read(&#39;config.ini&#39;)\n\nserver = config[&#39;DEFAULT&#39;][&#39;Server&#39;]\nport = config[&#39;DEFAULT&#39;][&#39;Port&#39;]\nprint(f&#39;Server: {server}, Port: {port}&#39;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u8bbf\u95ee\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u5404\u4e2a\u503c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u4f7f\u7528JSON\u683c\u5f0f\u7684\u914d\u7f6e\u6587\u4ef6\u6709\u4ec0\u4e48\u4f18\u52bf\uff1f<\/strong><br \/>\u4f7f\u7528JSON\u683c\u5f0f\u7684\u914d\u7f6e\u6587\u4ef6\u5177\u6709\u591a\u79cd\u4f18\u70b9\u3002JSON\u6587\u4ef6\u6613\u4e8e\u9605\u8bfb\u548c\u7f16\u8f91\uff0c\u80fd\u591f\u652f\u6301\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5982\u5d4c\u5957\u5bf9\u8c61\u548c\u6570\u7ec4\u3002\u6b64\u5916\uff0cPython\u63d0\u4f9b\u4e86\u5185\u7f6e\u7684<code>json<\/code>\u6a21\u5757\uff0c\u65b9\u4fbf\u89e3\u6790\u548c\u751f\u6210JSON\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u548c\u8bfb\u53d6JSON\u683c\u5f0f\u7684\u914d\u7f6e\u6587\u4ef6\uff1a  <\/p>\n<pre><code class=\"language-python\">import json\n\n# \u521b\u5efaJSON\u914d\u7f6e\u6587\u4ef6\nconfig_data = {\n    &quot;server&quot;: &quot;localhost&quot;,\n    &quot;port&quot;: 8080,\n    &quot;database&quot;: {\n        &quot;user&quot;: &quot;admin&quot;,\n        &quot;password&quot;: &quot;secret&quot;\n    }\n}\n\nwith open(&#39;config.json&#39;, &#39;w&#39;) as json_file:\n    json.dump(config_data, json_file, indent=4)\n\n# \u8bfb\u53d6JSON\u914d\u7f6e\u6587\u4ef6\nwith open(&#39;config.json&#39;, &#39;r&#39;) as json_file:\n    config = json.load(json_file)\n    print(config[&#39;server&#39;], config[&#39;port&#39;])\n<\/code><\/pre>\n<p>\u4f7f\u7528JSON\u683c\u5f0f\u80fd\u591f\u4f7f\u5f97\u914d\u7f6e\u7ba1\u7406\u66f4\u52a0\u7075\u6d3b\uff0c\u5c24\u5176\u9002\u5408\u9700\u8981\u590d\u6742\u6570\u636e\u7ed3\u6784\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u7f16\u5199\u914d\u7f6e\u6587\u4ef6\uff08conf\uff09\u901a\u5e38\u6709\u591a\u79cd\u65b9\u5f0f\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u3001\u73af\u5883\u53d8\u91cf\u3001\u547d\u4ee4\u884c\u53c2\u6570\u7b49\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1162430,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1162420"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1162420"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1162420\/revisions"}],"predecessor-version":[{"id":1162433,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1162420\/revisions\/1162433"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1162430"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1162420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1162420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1162420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}