{"id":1078288,"date":"2025-01-08T12:10:09","date_gmt":"2025-01-08T04:10:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1078288.html"},"modified":"2025-01-08T12:10:11","modified_gmt":"2025-01-08T04:10:11","slug":"python%e5%a6%82%e4%bd%95%e4%bd%bf%e8%be%93%e5%87%ba%e6%b2%a1%e6%9c%89%e9%94%ae%e5%80%bc%e5%af%b9-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1078288.html","title":{"rendered":"python\u5982\u4f55\u4f7f\u8f93\u51fa\u6ca1\u6709\u952e\u503c\u5bf9"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181925\/94139380-a505-441f-a6e9-24cb4ea89a73.webp\" alt=\"python\u5982\u4f55\u4f7f\u8f93\u51fa\u6ca1\u6709\u952e\u503c\u5bf9\" \/><\/p>\n<p><p> \u4e00\u3001Python\u5982\u4f55\u4f7f\u8f93\u51fa\u6ca1\u6709\u952e\u503c\u5bf9\uff1a\u4f7f\u7528\u5217\u8868\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001JSON\u5e8f\u5217\u5316<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5982\u679c\u4f60\u4e0d\u5e0c\u671b\u8f93\u51fa\u7ed3\u679c\u4e2d\u5305\u542b\u952e\u503c\u5bf9\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5217\u8868\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001JSON\u5e8f\u5217\u5316\u7b49\u65b9\u6cd5\u3002<strong>\u4f7f\u7528\u5217\u8868\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001JSON\u5e8f\u5217\u5316<\/strong> \u662f\u5e38\u89c1\u7684\u89e3\u51b3\u65b9\u6848\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u6765\u4f7f\u8f93\u51fa\u6ca1\u6709\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u53ef\u4ee5\u5c06\u6570\u636e\u4ee5\u7279\u5b9a\u7684\u683c\u5f0f\u8f93\u51fa\uff0c\u907f\u514d\u76f4\u63a5\u4f7f\u7528\u5b57\u5178\u7684\u952e\u503c\u5bf9\u5f62\u5f0f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>format()<\/code> \u65b9\u6cd5\u6216 f-strings \u6765\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3002\u5047\u8bbe\u6709\u4e00\u4e2a\u5b57\u5178 <code>data<\/code>\uff0c\u6211\u4eec\u5e0c\u671b\u8f93\u51fa\u5176\u4e2d\u7684\u503c\u800c\u4e0d\u662f\u952e\u503c\u5bf9\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u65b9\u5f0f\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>output = &quot;Name: {name}, Age: {age}, City: {city}&quot;.format(name=data[&#39;name&#39;], age=data[&#39;age&#39;], city=data[&#39;city&#39;])<\/p>\n<p>print(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>Name: Alice, Age: 25, City: New York<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\uff0c\u907f\u514d\u4e86\u76f4\u63a5\u8f93\u51fa\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4ed6\u51e0\u79cd\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5728\u4e0d\u540c\u573a\u666f\u4e0b\u5982\u4f55\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u907f\u514d\u952e\u503c\u5bf9\u8f93\u51fa\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5217\u8868<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5217\u8868\u662f\u53e6\u4e00\u79cd\u907f\u514d\u8f93\u51fa\u952e\u503c\u5bf9\u7684\u6709\u6548\u65b9\u6cd5\u3002\u5217\u8868\u53ef\u4ee5\u5b58\u50a8\u591a\u4e2a\u503c\uff0c\u8f93\u51fa\u65f6\u4e0d\u6d89\u53ca\u952e\u503c\u5bf9\u7684\u683c\u5f0f\u3002\u4e0b\u9762\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>output_list = [data[&#39;name&#39;], data[&#39;age&#39;], data[&#39;city&#39;]]<\/p>\n<p>print(output_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>[&#39;Alice&#39;, 25, &#39;New York&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u5c06\u5b57\u5178\u4e2d\u7684\u503c\u63d0\u53d6\u51fa\u6765\u5e76\u5b58\u50a8\u5728\u5217\u8868\u4e2d\uff0c\u7136\u540e\u8f93\u51fa\u5217\u8868\u3002\u8fd9\u6837\u5c31\u907f\u514d\u4e86\u952e\u503c\u5bf9\u7684\u663e\u793a\u3002<\/p>\n<\/p>\n<p><p>\u5217\u8868\u8fd8\u53ef\u4ee5\u8fdb\u884c\u66f4\u590d\u6742\u7684\u5904\u7406\uff0c\u4f8b\u5982\u904d\u5386\u6216\u8fdb\u884c\u6279\u91cf\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u5b57\u5178\u7684\u5217\u8868\uff0c\u53ef\u4ee5\u5c06\u6240\u6709\u5b57\u5178\u4e2d\u7684\u503c\u63d0\u53d6\u51fa\u6765\u5e76\u7ec4\u5408\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data_list = [<\/p>\n<p>    {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;},<\/p>\n<p>    {&#39;name&#39;: &#39;Bob&#39;, &#39;age&#39;: 30, &#39;city&#39;: &#39;Los Angeles&#39;},<\/p>\n<p>    {&#39;name&#39;: &#39;Charlie&#39;, &#39;age&#39;: 35, &#39;city&#39;: &#39;Chicago&#39;}<\/p>\n<p>]<\/p>\n<p>output_list = []<\/p>\n<p>for data in data_list:<\/p>\n<p>    output_list.append([data[&#39;name&#39;], data[&#39;age&#39;], data[&#39;city&#39;]])<\/p>\n<p>print(output_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>[[&#39;Alice&#39;, 25, &#39;New York&#39;], [&#39;Bob&#39;, 30, &#39;Los Angeles&#39;], [&#39;Charlie&#39;, 35, &#39;Chicago&#39;]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u7684\u573a\u666f\uff0c\u901a\u8fc7\u5c06\u5b57\u5178\u4e2d\u7684\u503c\u63d0\u53d6\u5e76\u5b58\u50a8\u5728\u5217\u8868\u4e2d\uff0c\u907f\u514d\u4e86\u952e\u503c\u5bf9\u7684\u663e\u793a\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316<\/p>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u662f\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\u7684\u5f3a\u5927\u5de5\u5177\u3002Python \u63d0\u4f9b\u4e86\u591a\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u5305\u62ec <code>%<\/code> \u64cd\u4f5c\u7b26\u3001<code>str.format()<\/code> \u65b9\u6cd5\u548c f-strings\u3002\u4e0b\u9762\u6211\u4eec\u5206\u522b\u4ecb\u7ecd\u8fd9\u4e09\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<ol>\n<li><code>%<\/code> \u64cd\u4f5c\u7b26<\/li>\n<\/ol>\n<p><p><code>%<\/code> \u64cd\u4f5c\u7b26\u662f\u8f83\u65e9\u671f\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u901a\u8fc7\u6307\u5b9a\u683c\u5f0f\u5316\u5b57\u7b26\u6765\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>output = &quot;Name: %s, Age: %d, City: %s&quot; % (data[&#39;name&#39;], data[&#39;age&#39;], data[&#39;city&#39;])<\/p>\n<p>print(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>Name: Alice, Age: 25, City: New York<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><code>str.format()<\/code> \u65b9\u6cd5<\/li>\n<\/ol>\n<p><p><code>str.format()<\/code> \u65b9\u6cd5\u662f Python 2.7 \u548c 3.0 \u5f15\u5165\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u5177\u6709\u66f4\u5f3a\u7684\u7075\u6d3b\u6027\u548c\u53ef\u8bfb\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>output = &quot;Name: {name}, Age: {age}, City: {city}&quot;.format(name=data[&#39;name&#39;], age=data[&#39;age&#39;], city=data[&#39;city&#39;])<\/p>\n<p>print(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>Name: Alice, Age: 25, City: New York<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>f-strings<\/li>\n<\/ol>\n<p><p>f-strings \u662f Python 3.6 \u5f15\u5165\u7684\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff0c\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u524d\u6dfb\u52a0 <code>f<\/code> \u5e76\u5728\u5927\u62ec\u53f7\u5185\u5d4c\u5165\u53d8\u91cf\u540d\u6765\u5b9e\u73b0\u683c\u5f0f\u5316\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>output = f&quot;Name: {data[&#39;name&#39;]}, Age: {data[&#39;age&#39;]}, City: {data[&#39;city&#39;]}&quot;<\/p>\n<p>print(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>Name: Alice, Age: 25, City: New York<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u90fd\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\uff0c\u907f\u514d\u663e\u793a\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u56db\u3001\u4f7f\u7528JSON\u5e8f\u5217\u5316<\/p>\n<\/p>\n<p><p>JSON\uff08JavaScript Object Notation\uff09\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u5e38\u7528\u4e8e\u6570\u636e\u4f20\u8f93\u548c\u5b58\u50a8\u3002Python \u63d0\u4f9b\u4e86\u5185\u7f6e\u7684 <code>json<\/code> \u6a21\u5757\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c JSON \u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u3002\u901a\u8fc7\u5c06\u5b57\u5178\u5e8f\u5217\u5316\u4e3a JSON \u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u907f\u514d\u952e\u503c\u5bf9\u7684\u76f4\u63a5\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>output = json.dumps(data, separators=(&#39;,&#39;, &#39;:&#39;))<\/p>\n<p>print(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>{&quot;name&quot;:&quot;Alice&quot;,&quot;age&quot;:25,&quot;city&quot;:&quot;New York&quot;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>json.dumps()<\/code> \u51fd\u6570\u5c06\u5b57\u5178\u5e8f\u5217\u5316\u4e3a JSON \u5b57\u7b26\u4e32\uff0c\u5e76\u6307\u5b9a <code>separators<\/code> \u53c2\u6570\u6765\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\u3002<code>separators=(&#39;,&#39;, &#39;:&#39;)<\/code> \u53c2\u6570\u6307\u5b9a\u5728 JSON \u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u9017\u53f7\u548c\u5192\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26\uff0c\u4ece\u800c\u907f\u514d\u663e\u793a\u7a7a\u683c\u548c\u5176\u4ed6\u5197\u4f59\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>json.loads()<\/code> \u51fd\u6570\u5c06 JSON \u5b57\u7b26\u4e32\u53cd\u5e8f\u5217\u5316\u4e3a Python \u5bf9\u8c61\uff0c\u65b9\u4fbf\u6570\u636e\u7684\u8fdb\u4e00\u6b65\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">json_str = &#39;{&quot;name&quot;:&quot;Alice&quot;,&quot;age&quot;:25,&quot;city&quot;:&quot;New York&quot;}&#39;<\/p>\n<p>data = json.loads(json_str)<\/p>\n<p>print(data[&#39;name&#39;], data[&#39;age&#39;], data[&#39;city&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>Alice 25 New York<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5728\u4e0d\u540c\u7cfb\u7edf\u6216\u5e73\u53f0\u4e4b\u95f4\u4f20\u8f93\u6570\u636e\u7684\u573a\u666f\uff0c\u901a\u8fc7 JSON \u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u636e\u4ea4\u6362\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u4e94\u3001\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/p>\n<\/p>\n<p><p>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u8fed\u4ee3\u5668\uff0c\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u6309\u9700\u751f\u6210\u6570\u636e\uff0c\u907f\u514d\u4e00\u6b21\u6027\u52a0\u8f7d\u6240\u6709\u6570\u636e\u3002\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u53ef\u4ee5\u907f\u514d\u952e\u503c\u5bf9\u7684\u76f4\u63a5\u8f93\u51fa\uff0c\u5e76\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u8282\u7701\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>output = (f&quot;{key}: {value}&quot; for key, value in data.items())<\/p>\n<p>for item in output:<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>name: Alice<\/p>\n<p>age: 25<\/p>\n<p>city: New York<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u5c06\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u683c\u5f0f\u5316\u4e3a\u5b57\u7b26\u4e32\uff0c\u5e76\u9010\u9879\u8f93\u51fa\u3002\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u7684\u597d\u5904\u662f\u6309\u9700\u751f\u6210\u6570\u636e\uff0c\u8282\u7701\u5185\u5b58\uff0c\u540c\u65f6\u907f\u514d\u4e86\u952e\u503c\u5bf9\u7684\u76f4\u63a5\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u9010\u9879\u5904\u7406\u6216\u8f93\u51fa\u6570\u636e\u7684\u573a\u666f\uff0c\u901a\u8fc7\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\uff0c\u5e76\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u516d\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u81ea\u5b9a\u4e49\u51fd\u6570\u662f\u4e00\u79cd\u7075\u6d3b\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u7f16\u5199\u51fd\u6570\u6765\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\u3002\u901a\u8fc7\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u903b\u8f91\u548c\u5904\u7406\uff0c\u907f\u514d\u952e\u503c\u5bf9\u7684\u76f4\u63a5\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def format_data(data):<\/p>\n<p>    return f&quot;Name: {data[&#39;name&#39;]}, Age: {data[&#39;age&#39;]}, City: {data[&#39;city&#39;]}&quot;<\/p>\n<p>data = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>output = format_data(data)<\/p>\n<p>print(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>Name: Alice, Age: 25, City: New York<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a <code>format_data()<\/code> \u51fd\u6570\uff0c\u7528\u4e8e\u683c\u5f0f\u5316\u5b57\u5178\u4e2d\u7684\u6570\u636e\u5e76\u8fd4\u56de\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3002\u901a\u8fc7\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\uff0c\u5e76\u907f\u514d\u952e\u503c\u5bf9\u7684\u76f4\u63a5\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><p>\u81ea\u5b9a\u4e49\u51fd\u6570\u8fd8\u53ef\u4ee5\u5904\u7406\u66f4\u590d\u6742\u7684\u903b\u8f91\u548c\u9700\u6c42\uff0c\u4f8b\u5982\u5904\u7406\u5d4c\u5957\u5b57\u5178\u6216\u8fdb\u884c\u6570\u636e\u9a8c\u8bc1\u548c\u8f6c\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def format_data(data):<\/p>\n<p>    if &#39;name&#39; in data and &#39;age&#39; in data and &#39;city&#39; in data:<\/p>\n<p>        return f&quot;Name: {data[&#39;name&#39;]}, Age: {data[&#39;age&#39;]}, City: {data[&#39;city&#39;]}&quot;<\/p>\n<p>    else:<\/p>\n<p>        return &quot;Invalid data&quot;<\/p>\n<p>data = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>output = format_data(data)<\/p>\n<p>print(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>Name: Alice, Age: 25, City: New York<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u7075\u6d3b\u5730\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\u548c\u5904\u7406\u903b\u8f91\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u590d\u6742\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u4e03\u3001\u7ed3\u8bba<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u907f\u514d\u8f93\u51fa\u952e\u503c\u5bf9\uff0c\u5305\u62ec\u4f7f\u7528\u5217\u8868\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001JSON\u5e8f\u5217\u5316\u3001\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u548c\u81ea\u5b9a\u4e49\u51fd\u6570\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u70b9\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u7075\u6d3b\u5730\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u5217\u8868\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001JSON\u5e8f\u5217\u5316<\/strong> \u662f\u6700\u5e38\u89c1\u548c\u63a8\u8350\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u907f\u514d\u952e\u503c\u5bf9\u7684\u76f4\u63a5\u8f93\u51fa\uff0c\u5e76\u5728\u5904\u7406\u6570\u636e\u65f6\u63d0\u4f9b\u826f\u597d\u7684\u7075\u6d3b\u6027\u548c\u53ef\u8bfb\u6027\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u80fd\u591f\u5e2e\u52a9\u4f60\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\u66f4\u597d\u5730\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\uff0c\u63d0\u5347\u4ee3\u7801\u7684\u8d28\u91cf\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8f93\u51fa\u4e0d\u5e26\u952e\u503c\u5bf9\u7684\u5b57\u5178\u5185\u5bb9\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u5b57\u5178\u5e76\u5355\u72ec\u8f93\u51fa\u6bcf\u4e2a\u952e\u6216\u503c\u6765\u5b9e\u73b0\u4e0d\u5e26\u952e\u503c\u5bf9\u7684\u8f93\u51fa\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u904d\u5386\u5b57\u5178\u7684<code>keys()<\/code>\u6216<code>values()<\/code>\u65b9\u6cd5\uff0c\u6216\u8005\u76f4\u63a5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u904d\u5386\u5b57\u5178\u5bf9\u8c61\u3002\u5982\u679c\u5e0c\u671b\u4ee5\u7279\u5b9a\u683c\u5f0f\u8f93\u51fa\uff0c\u53ef\u4ee5\u5c06\u7ed3\u679c\u62fc\u63a5\u6210\u5b57\u7b26\u4e32\uff0c\u6216\u8005\u4f7f\u7528<code>join()<\/code>\u65b9\u6cd5\u5c06\u591a\u4e2a\u503c\u8fde\u63a5\u8d77\u6765\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u7528\u4ec0\u4e48\u65b9\u6cd5\u6765\u683c\u5f0f\u5316\u8f93\u51faPython\u5b57\u5178\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u7684<code>dumps<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u5178\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5e76\u683c\u5f0f\u5316\u8f93\u51fa\u3002\u901a\u8fc7\u8bbe\u7f6e\u53c2\u6570<code>indent<\/code>\uff0c\u53ef\u4ee5\u63a7\u5236\u7f29\u8fdb\u7684\u7a7a\u683c\u6570\uff0c\u4f7f\u8f93\u51fa\u66f4\u5177\u53ef\u8bfb\u6027\u3002\u6b64\u5916\uff0c\u7ed3\u5408<code>pprint<\/code>\u6a21\u5757\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u66f4\u4e3a\u7f8e\u89c2\u7684\u8f93\u51fa\u683c\u5f0f\uff0c\u9002\u5408\u8c03\u8bd5\u548c\u67e5\u770b\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u8f93\u51fa\u5b57\u5178\u7684\u503c\uff1f<\/strong><br \/>\u7edd\u5bf9\u53ef\u4ee5\uff0c\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u5feb\u901f\u751f\u6210\u4e00\u4e2a\u5305\u542b\u5b57\u5178\u6240\u6709\u503c\u7684\u5217\u8868\u3002\u4f8b\u5982\uff0c<code>[value for value in my_dict.values()]<\/code>\u53ef\u4ee5\u63d0\u53d6\u51fa\u6240\u6709\u7684\u503c\u3002\u5982\u679c\u9700\u8981\u8fdb\u4e00\u6b65\u5904\u7406\u8fd9\u4e9b\u503c\uff0c\u4e5f\u53ef\u4ee5\u5728\u5217\u8868\u63a8\u5bfc\u5f0f\u4e2d\u6dfb\u52a0\u6761\u4ef6\u6216\u5176\u4ed6\u64cd\u4f5c\uff0c\u4f7f\u5f97\u8f93\u51fa\u66f4\u52a0\u7075\u6d3b\u4e0e\u5f3a\u5927\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e00\u3001Python\u5982\u4f55\u4f7f\u8f93\u51fa\u6ca1\u6709\u952e\u503c\u5bf9\uff1a\u4f7f\u7528\u5217\u8868\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001JSON\u5e8f\u5217\u5316 \u5728Python\u4e2d\uff0c\u5982\u679c\u4f60\u4e0d\u5e0c\u671b\u8f93 [&hellip;]","protected":false},"author":3,"featured_media":1078299,"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\/1078288"}],"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=1078288"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1078288\/revisions"}],"predecessor-version":[{"id":1078300,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1078288\/revisions\/1078300"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1078299"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1078288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1078288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1078288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}