{"id":1173295,"date":"2025-01-15T17:01:25","date_gmt":"2025-01-15T09:01:25","guid":{"rendered":""},"modified":"2025-01-15T17:01:27","modified_gmt":"2025-01-15T09:01:27","slug":"python%e5%a4%9a%e5%b1%82%e5%ad%97%e5%85%b8%e5%a6%82%e4%bd%95%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1173295.html","title":{"rendered":"python\u591a\u5c42\u5b57\u5178\u5982\u4f55\u89e3\u6790"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26075126\/16f05f3d-ddbe-41b9-8716-7714e411285e.webp\" alt=\"python\u591a\u5c42\u5b57\u5178\u5982\u4f55\u89e3\u6790\" \/><\/p>\n<p><p> <strong>Python\u591a\u5c42\u5b57\u5178\u7684\u89e3\u6790\u53ef\u4ee5\u901a\u8fc7<\/strong>\u9012\u5f52\u904d\u5386\u3001\u8fed\u4ee3\u65b9\u6cd5\u3001\u4f7f\u7528\u5185\u7f6e\u5e93\u7b49<strong>\u5b9e\u73b0<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u9012\u5f52\u904d\u5386<\/strong>\u662f\u4e00\u79cd\u8f83\u4e3a\u901a\u7528\u4e14\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u9012\u5f52\u904d\u5386\u7684\u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u5728\u51fd\u6570\u5185\u90e8\u68c0\u6d4b\u5f53\u524d\u5b57\u5178\u7684\u503c\u662f\u5426\u8fd8\u662f\u5b57\u5178\uff0c\u5982\u679c\u662f\uff0c\u5219\u8c03\u7528\u81ea\u8eab\u51fd\u6570\u7ee7\u7eed\u904d\u5386\uff0c\u76f4\u5230\u9047\u5230\u975e\u5b57\u5178\u7684\u503c\u4e3a\u6b62\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u901a\u8fc7\u9012\u5f52\u904d\u5386\u89e3\u6790\u591a\u5c42\u5b57\u5178\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u9012\u5f52\u904d\u5386\u89e3\u6790\u591a\u5c42\u5b57\u5178<\/strong><\/h2>\n<p><p>\u9012\u5f52\u904d\u5386\u662f\u4e00\u79cd\u975e\u5e38\u9002\u5408\u5904\u7406\u591a\u5c42\u5d4c\u5957\u7ed3\u6784\u7684\u65b9\u6cd5\u3002\u9012\u5f52\u7684\u6838\u5fc3\u601d\u60f3\u662f\u51fd\u6570\u8c03\u7528\u81ea\u8eab\uff0c\u901a\u8fc7\u4e0d\u65ad\u5730\u5c06\u95ee\u9898\u89c4\u6a21\u7f29\u5c0f\uff0c\u6700\u7ec8\u8fbe\u5230\u89e3\u7684\u76ee\u7684\u3002\u5bf9\u4e8e\u591a\u5c42\u5b57\u5178\uff0c\u9012\u5f52\u904d\u5386\u7684\u601d\u8def\u662f\uff1a\u5982\u679c\u5f53\u524d\u5c42\u7684\u67d0\u4e2a\u503c\u8fd8\u662f\u5b57\u5178\uff0c\u5c31\u5bf9\u8fd9\u4e2a\u503c\u8c03\u7528\u9012\u5f52\u51fd\u6570\uff1b\u5982\u679c\u4e0d\u662f\u5b57\u5178\uff0c\u5c31\u5904\u7406\u8fd9\u4e2a\u503c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def parse_dict(d, parent_key=&#39;&#39;):<\/p>\n<p>    items = []<\/p>\n<p>    for k, v in d.items():<\/p>\n<p>        new_key = f&quot;{parent_key}.{k}&quot; if parent_key else k<\/p>\n<p>        if isinstance(v, dict):<\/p>\n<p>            items.extend(parse_dict(v, new_key).items())<\/p>\n<p>        else:<\/p>\n<p>            items.append((new_key, v))<\/p>\n<p>    return dict(items)<\/p>\n<p>nested_dict = {<\/p>\n<p>    &#39;key1&#39;: 1,<\/p>\n<p>    &#39;key2&#39;: {<\/p>\n<p>        &#39;key2_1&#39;: 21,<\/p>\n<p>        &#39;key2_2&#39;: {<\/p>\n<p>            &#39;key2_2_1&#39;: 221,<\/p>\n<p>        },<\/p>\n<p>    },<\/p>\n<p>    &#39;key3&#39;: 3,<\/p>\n<p>}<\/p>\n<p>parsed_dict = parse_dict(nested_dict)<\/p>\n<p>print(parsed_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001\u8fed\u4ee3\u65b9\u6cd5\u89e3\u6790\u591a\u5c42\u5b57\u5178<\/strong><\/h2>\n<p><p>\u9664\u4e86\u9012\u5f52\u904d\u5386\u5916\uff0c\u4f7f\u7528\u8fed\u4ee3\u65b9\u6cd5\u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u89e3\u6790\u591a\u5c42\u5b57\u5178\u7684\u65b9\u5f0f\u3002\u8fed\u4ee3\u65b9\u6cd5\u901a\u5e38\u501f\u52a9\u6808\uff08stack\uff09\u6216\u8005\u961f\u5217\uff08queue\uff09\uff0c\u9010\u5c42\u89e3\u6790\u5b57\u5178\u3002\u901a\u8fc7\u4e0d\u65ad\u5730\u5c06\u5f53\u524d\u5c42\u7684\u952e\u503c\u5bf9\u63a8\u5165\u6808\u6216\u8005\u961f\u5217\uff0c\u9010\u6b65\u5c55\u5f00\u5d4c\u5957\u7684\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u4f7f\u7528\u6808\u89e3\u6790\u591a\u5c42\u5b57\u5178<\/h2>\n<\/p>\n<p><p>\u6808\u662f\u4e00\u79cd\u540e\u8fdb\u5148\u51fa\u7684\u6570\u636e\u7ed3\u6784\uff0c\u975e\u5e38\u9002\u5408\u7528\u4e8e\u904d\u5386\u548c\u89e3\u6790\u591a\u5c42\u5d4c\u5957\u7ed3\u6784\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u6808\u89e3\u6790\u591a\u5c42\u5b57\u5178\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def parse_dict_with_stack(d):<\/p>\n<p>    stack = [(d, &#39;&#39;)]<\/p>\n<p>    items = []<\/p>\n<p>    while stack:<\/p>\n<p>        current, parent_key = stack.pop()<\/p>\n<p>        for k, v in current.items():<\/p>\n<p>            new_key = f&quot;{parent_key}.{k}&quot; if parent_key else k<\/p>\n<p>            if isinstance(v, dict):<\/p>\n<p>                stack.append((v, new_key))<\/p>\n<p>            else:<\/p>\n<p>                items.append((new_key, v))<\/p>\n<p>    return dict(items)<\/p>\n<p>parsed_dict = parse_dict_with_stack(nested_dict)<\/p>\n<p>print(parsed_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u4f7f\u7528\u961f\u5217\u89e3\u6790\u591a\u5c42\u5b57\u5178<\/h2>\n<\/p>\n<p><p>\u961f\u5217\u662f\u4e00\u79cd\u5148\u8fdb\u5148\u51fa\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u89e3\u6790\u591a\u5c42\u5b57\u5178\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u961f\u5217\u89e3\u6790\u591a\u5c42\u5b57\u5178\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import deque<\/p>\n<p>def parse_dict_with_queue(d):<\/p>\n<p>    queue = deque([(d, &#39;&#39;)])<\/p>\n<p>    items = []<\/p>\n<p>    while queue:<\/p>\n<p>        current, parent_key = queue.popleft()<\/p>\n<p>        for k, v in current.items():<\/p>\n<p>            new_key = f&quot;{parent_key}.{k}&quot; if parent_key else k<\/p>\n<p>            if isinstance(v, dict):<\/p>\n<p>                queue.append((v, new_key))<\/p>\n<p>            else:<\/p>\n<p>                items.append((new_key, v))<\/p>\n<p>    return dict(items)<\/p>\n<p>parsed_dict = parse_dict_with_queue(nested_dict)<\/p>\n<p>print(parsed_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001\u4f7f\u7528\u5185\u7f6e\u5e93\u89e3\u6790\u591a\u5c42\u5b57\u5178<\/strong><\/h2>\n<p><p>Python \u63d0\u4f9b\u4e86\u4e00\u4e9b\u5f3a\u5927\u7684\u5185\u7f6e\u5e93\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u89e3\u6790\u548c\u64cd\u4f5c\u591a\u5c42\u5d4c\u5957\u7684\u5b57\u5178\u7ed3\u6784\u3002\u5e38\u7528\u7684\u5e93\u6709 <code>json<\/code> \u548c <code>pprint<\/code>\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u4f7f\u7528 <code>json<\/code> \u5e93\u89e3\u6790\u591a\u5c42\u5b57\u5178<\/h2>\n<\/p>\n<p><p><code>json<\/code> \u5e93\u4e3b\u8981\u7528\u4e8e\u5904\u7406 JSON \u6570\u636e\uff0c\u4f46\u7531\u4e8e JSON \u6570\u636e\u7ed3\u6784\u4e0e Python \u7684\u5b57\u5178\u7ed3\u6784\u975e\u5e38\u76f8\u4f3c\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>json<\/code> \u5e93\u6765\u89e3\u6790\u548c\u5904\u7406\u591a\u5c42\u5b57\u5178\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>nested_dict = {<\/p>\n<p>    &#39;key1&#39;: 1,<\/p>\n<p>    &#39;key2&#39;: {<\/p>\n<p>        &#39;key2_1&#39;: 21,<\/p>\n<p>        &#39;key2_2&#39;: {<\/p>\n<p>            &#39;key2_2_1&#39;: 221,<\/p>\n<p>        },<\/p>\n<p>    },<\/p>\n<p>    &#39;key3&#39;: 3,<\/p>\n<p>}<\/p>\n<p>json_string = json.dumps(nested_dict, indent=4)<\/p>\n<p>print(json_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u4f7f\u7528 <code>pprint<\/code> \u5e93\u89e3\u6790\u591a\u5c42\u5b57\u5178<\/h2>\n<\/p>\n<p><p><code>pprint<\/code> \u5e93\u63d0\u4f9b\u4e86\u7f8e\u89c2\u6253\u5370\u6570\u636e\u7ed3\u6784\u7684\u529f\u80fd\uff0c\u5bf9\u4e8e\u591a\u5c42\u5d4c\u5957\u5b57\u5178\u975e\u5e38\u6709\u7528\u3002\u4f7f\u7528 <code>pprint<\/code> \u5e93\u53ef\u4ee5\u66f4\u76f4\u89c2\u5730\u67e5\u770b\u5b57\u5178\u7ed3\u6784\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pprint import pprint<\/p>\n<p>nested_dict = {<\/p>\n<p>    &#39;key1&#39;: 1,<\/p>\n<p>    &#39;key2&#39;: {<\/p>\n<p>        &#39;key2_1&#39;: 21,<\/p>\n<p>        &#39;key2_2&#39;: {<\/p>\n<p>            &#39;key2_2_1&#39;: 221,<\/p>\n<p>        },<\/p>\n<p>    },<\/p>\n<p>    &#39;key3&#39;: 3,<\/p>\n<p>}<\/p>\n<p>pprint(nested_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u89e3\u6790<\/strong><\/h2>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u89e3\u6790\u591a\u5c42\u5b57\u5178\u4ee5\u63d0\u53d6\u7279\u5b9a\u4fe1\u606f\u6216\u8fdb\u884c\u6570\u636e\u5904\u7406\u3002\u4e0b\u9762\u5217\u51fa\u4e00\u4e9b\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\uff0c\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u89e3\u51b3\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u914d\u7f6e\u6587\u4ef6\u89e3\u6790<\/h2>\n<\/p>\n<p><p>\u5728\u8bb8\u591a\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u914d\u7f6e\u6587\u4ef6\u901a\u5e38\u91c7\u7528\u5d4c\u5957\u5b57\u5178\u7684\u7ed3\u6784\u6765\u8868\u793a\u914d\u7f6e\u9879\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u89e3\u6790\u914d\u7f6e\u6587\u4ef6\uff0c\u5e76\u63d0\u53d6\u6240\u9700\u7684\u914d\u7f6e\u9879\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">config = {<\/p>\n<p>    &#39;database&#39;: {<\/p>\n<p>        &#39;host&#39;: &#39;localhost&#39;,<\/p>\n<p>        &#39;port&#39;: 3306,<\/p>\n<p>        &#39;credentials&#39;: {<\/p>\n<p>            &#39;user&#39;: &#39;root&#39;,<\/p>\n<p>            &#39;password&#39;: &#39;password&#39;<\/p>\n<p>        }<\/p>\n<p>    },<\/p>\n<p>    &#39;logging&#39;: {<\/p>\n<p>        &#39;level&#39;: &#39;DEBUG&#39;,<\/p>\n<p>        &#39;handlers&#39;: [&#39;console&#39;, &#39;file&#39;]<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>def get_config_value(config, key_path):<\/p>\n<p>    keys = key_path.split(&#39;.&#39;)<\/p>\n<p>    value = config<\/p>\n<p>    for key in keys:<\/p>\n<p>        value = value[key]<\/p>\n<p>    return value<\/p>\n<p>db_host = get_config_value(config, &#39;database.host&#39;)<\/p>\n<p>log_level = get_config_value(config, &#39;logging.level&#39;)<\/p>\n<p>print(f&quot;Database Host: {db_host}&quot;)<\/p>\n<p>print(f&quot;Log Level: {log_level}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u5904\u7406 JSON \u6570\u636e<\/h2>\n<\/p>\n<p><p>\u5728 Web \u5f00\u53d1\u548c\u6570\u636e\u5904\u7406\u9886\u57df\uff0cJSON \u6570\u636e\u683c\u5f0f\u88ab\u5e7f\u6cdb\u4f7f\u7528\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u7684\u65b9\u6cd5\u89e3\u6790\u548c\u5904\u7406\u591a\u5c42 JSON \u6570\u636e\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>json_data = &#39;&#39;&#39;<\/p>\n<p>{<\/p>\n<p>    &quot;user&quot;: {<\/p>\n<p>        &quot;id&quot;: 1,<\/p>\n<p>        &quot;name&quot;: &quot;John Doe&quot;,<\/p>\n<p>        &quot;contact&quot;: {<\/p>\n<p>            &quot;em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l&quot;: &quot;john.doe@example.com&quot;,<\/p>\n<p>            &quot;phone&quot;: &quot;123-456-7890&quot;<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>&#39;&#39;&#39;<\/p>\n<p>data = json.loads(json_data)<\/p>\n<p>def get_json_value(data, key_path):<\/p>\n<p>    keys = key_path.split(&#39;.&#39;)<\/p>\n<p>    value = data<\/p>\n<p>    for key in keys:<\/p>\n<p>        value = value[key]<\/p>\n<p>    return value<\/p>\n<p>user_name = get_json_value(data, &#39;user.name&#39;)<\/p>\n<p>user_email = get_json_value(data, &#39;user.contact.email&#39;)<\/p>\n<p>print(f&quot;User Name: {user_name}&quot;)<\/p>\n<p>print(f&quot;User Email: {user_email}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3\u3001\u6570\u636e\u6e05\u6d17\u4e0e\u8f6c\u6362<\/h2>\n<\/p>\n<p><p>\u5728\u6570\u636e\u79d1\u5b66\u548c\u6570\u636e\u5206\u6790\u9886\u57df\uff0c\u6570\u636e\u6e05\u6d17\u4e0e\u8f6c\u6362\u662f\u5e38\u89c1\u7684\u4efb\u52a1\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u89e3\u6790\u591a\u5c42\u5b57\u5178\u7684\u65b9\u6cd5\uff0c\u5c06\u5d4c\u5957\u7684\u6570\u636e\u7ed3\u6784\u8f6c\u6362\u4e3a\u6241\u5e73\u7ed3\u6784\uff0c\u4fbf\u4e8e\u540e\u7eed\u7684\u6570\u636e\u5904\u7406\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {<\/p>\n<p>    &#39;record1&#39;: {<\/p>\n<p>        &#39;name&#39;: &#39;Alice&#39;,<\/p>\n<p>        &#39;scores&#39;: {<\/p>\n<p>            &#39;math&#39;: 85,<\/p>\n<p>            &#39;science&#39;: 90<\/p>\n<p>        }<\/p>\n<p>    },<\/p>\n<p>    &#39;record2&#39;: {<\/p>\n<p>        &#39;name&#39;: &#39;Bob&#39;,<\/p>\n<p>        &#39;scores&#39;: {<\/p>\n<p>            &#39;math&#39;: 75,<\/p>\n<p>            &#39;science&#39;: 80<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>def flatten_data(data):<\/p>\n<p>    flat_data = {}<\/p>\n<p>    for record_key, record_value in data.items():<\/p>\n<p>        for key, value in record_value.items():<\/p>\n<p>            if isinstance(value, dict):<\/p>\n<p>                for sub_key, sub_value in value.items():<\/p>\n<p>                    flat_key = f&quot;{record_key}.{key}.{sub_key}&quot;<\/p>\n<p>                    flat_data[flat_key] = sub_value<\/p>\n<p>            else:<\/p>\n<p>                flat_key = f&quot;{record_key}.{key}&quot;<\/p>\n<p>                flat_data[flat_key] = value<\/p>\n<p>    return flat_data<\/p>\n<p>flattened_data = flatten_data(data)<\/p>\n<p>print(flattened_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e94\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u89e3\u6790\u591a\u5c42\u5b57\u5178\u662f Python \u7f16\u7a0b\u4e2d\u5e38\u89c1\u7684\u4efb\u52a1\uff0c\u638c\u63e1\u4e0d\u540c\u7684\u89e3\u6790\u65b9\u6cd5\u6709\u52a9\u4e8e\u6211\u4eec\u66f4\u9ad8\u6548\u5730\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002<strong>\u9012\u5f52\u904d\u5386\u3001\u8fed\u4ee3\u65b9\u6cd5\u3001\u4f7f\u7528\u5185\u7f6e\u5e93<\/strong>\u7b49\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u5177\u4f53\u9009\u62e9\u53d6\u51b3\u4e8e\u5b9e\u9645\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u7075\u6d3b\u5e94\u7528\u8fd9\u4e9b\u6280\u5de7\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u89e3\u6790\u548c\u5904\u7406\u591a\u5c42\u5b57\u5178\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u8be6\u7ec6\u4e86\u89e3\u4e86\u5982\u4f55\u901a\u8fc7\u9012\u5f52\u904d\u5386\u3001\u8fed\u4ee3\u65b9\u6cd5\uff08\u5305\u62ec\u4f7f\u7528\u6808\u548c\u961f\u5217\uff09\u4ee5\u53ca\u5185\u7f6e\u5e93\uff08\u5982 <code>json<\/code> \u548c <code>pprint<\/code>\uff09\u6765\u89e3\u6790\u591a\u5c42\u5b57\u5178\uff0c\u5e76\u7ed3\u5408\u5b9e\u9645\u5e94\u7528\u573a\u666f\u63d0\u4f9b\u4e86\u5177\u4f53\u7684\u89e3\u51b3\u65b9\u6848\u3002\u8fd9\u4e9b\u65b9\u6cd5\u548c\u6280\u5de7\u4e0d\u4ec5\u9002\u7528\u4e8e\u89e3\u6790\u591a\u5c42\u5b57\u5178\uff0c\u4e5f\u53ef\u4ee5\u6269\u5c55\u5e94\u7528\u4e8e\u5176\u4ed6\u590d\u6742\u6570\u636e\u7ed3\u6784\u7684\u5904\u7406\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u5728\u5904\u7406\u591a\u5c42\u5b57\u5178\u65f6\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bbf\u95ee\u591a\u5c42\u5b57\u5178\u7684\u7279\u5b9a\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8bbf\u95ee\u591a\u5c42\u5b57\u5178\u7684\u7279\u5b9a\u503c\u53ef\u4ee5\u901a\u8fc7\u591a\u91cd\u7d22\u5f15\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5b57\u5178<code>data<\/code>\uff0c\u5b83\u7684\u7ed3\u6784\u662f<code>{&#39;key1&#39;: {&#39;subkey1&#39;: &#39;value1&#39;, &#39;subkey2&#39;: &#39;value2&#39;}}<\/code>\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7<code>data[&#39;key1&#39;][&#39;subkey1&#39;]<\/code>\u6765\u83b7\u53d6<code>&#39;value1&#39;<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u5f97\u4f60\u80fd\u591f\u7cbe\u786e\u5730\u5b9a\u4f4d\u5230\u4f60\u9700\u8981\u7684\u6570\u636e\u3002<\/p>\n<p><strong>\u5728\u5904\u7406\u591a\u5c42\u5b57\u5178\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u89e3\u6790\u591a\u5c42\u5b57\u5178\u65f6\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u4f7f\u7528\u4e86\u9519\u8bef\u7684\u952e\u540d\u3001\u5c1d\u8bd5\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u5b50\u952e\u3001\u6216\u8005\u662f\u5bf9\u5b57\u5178\u7684\u7ed3\u6784\u7406\u89e3\u4e0d\u6e05\u3002\u4f7f\u7528<code>get()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u907f\u514dKeyError\uff0c\u4f8b\u5982<code>data.get(&#39;key1&#39;, {}).get(&#39;subkey1&#39;)<\/code>\uff0c\u8fd9\u6837\u5982\u679c<code>key1<\/code>\u4e0d\u5b58\u5728\uff0c\u4f1a\u8fd4\u56de<code>None<\/code>\u800c\u4e0d\u662f\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528\u5faa\u73af\u904d\u5386\u591a\u5c42\u5b57\u5178\u4e2d\u7684\u6240\u6709\u952e\u548c\u503c\uff1f<\/strong><br \/>\u904d\u5386\u591a\u5c42\u5b57\u5178\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u3002\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u6536\u5b57\u5178\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u904d\u5386\u5b57\u5178\u7684\u6bcf\u4e00\u4e2a\u952e\u503c\u5bf9\u3002\u5982\u679c\u503c\u662f\u53e6\u4e00\u4e2a\u5b57\u5178\uff0c\u5219\u9012\u5f52\u8c03\u7528\u8be5\u51fd\u6570\u3002\u8fd9\u6837\u7684\u5b9e\u73b0\u80fd\u591f\u6709\u6548\u5730\u5217\u51fa\u6240\u6709\u7684\u952e\u548c\u503c\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u5b57\u5178\u7ed3\u6784\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u591a\u5c42\u5b57\u5178\u7684\u89e3\u6790\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u904d\u5386\u3001\u8fed\u4ee3\u65b9\u6cd5\u3001\u4f7f\u7528\u5185\u7f6e\u5e93\u7b49\u5b9e\u73b0\u3002\u5176\u4e2d\uff0c\u9012\u5f52\u904d\u5386\u662f\u4e00\u79cd\u8f83\u4e3a\u901a\u7528\u4e14\u7b80\u5355\u7684\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":1173302,"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\/1173295"}],"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=1173295"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1173295\/revisions"}],"predecessor-version":[{"id":1173303,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1173295\/revisions\/1173303"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1173302"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1173295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1173295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1173295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}