{"id":1077698,"date":"2025-01-08T12:04:44","date_gmt":"2025-01-08T04:04:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1077698.html"},"modified":"2025-01-08T12:04:47","modified_gmt":"2025-01-08T04:04:47","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e7%ad%89%e5%8f%b7%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%ad%97%e5%85%b8-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1077698.html","title":{"rendered":"python\u5982\u4f55\u5c06\u7b49\u53f7\u8f6c\u6362\u4e3a\u5b57\u5178"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181636\/8e80b590-f96e-424d-a293-ce5f921a310a.webp\" alt=\"python\u5982\u4f55\u5c06\u7b49\u53f7\u8f6c\u6362\u4e3a\u5b57\u5178\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u5c06\u7b49\u53f7\u8f6c\u6362\u4e3a\u5b57\u5178\u7684\u6838\u5fc3\u65b9\u6cd5\u662f\uff1a\u4f7f\u7528\u5b57\u7b26\u4e32\u64cd\u4f5c\u548c\u5b57\u5178\u89e3\u6790\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u4f7f\u7528csv\u6a21\u5757<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u5b57\u7b26\u4e32\u64cd\u4f5c\u548c\u5b57\u5178\u89e3\u6790\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u5728\u7f16\u5199Python\u7a0b\u5e8f\u65f6\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u9700\u8981\u5c06\u952e\u503c\u5bf9\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u5178\u7684\u60c5\u51b5\u3002\u6bd4\u5982\uff0c\u5c06\u5b57\u7b26\u4e32\u201ckey1=value1;key2=value2\u201d\u8f6c\u6362\u4e3a\u5b57\u5178{&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;}\u3002\u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u51e0\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e2a\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u64cd\u4f5c\u548c\u5b57\u5178\u89e3\u6790<\/p>\n<\/p>\n<p><p>\u8fd9\u662f\u6700\u76f4\u63a5\u3001\u4e5f\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528Python\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u51fd\u6570\u548c\u5b57\u5178\u89e3\u6790\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><p><strong>\u6b65\u9aa4<\/strong>\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528\u5b57\u7b26\u4e32\u7684split()\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u6309\u5206\u53f7\u5206\u5272\u6210\u952e\u503c\u5bf9\u7684\u5217\u8868\u3002<\/li>\n<li>\u5bf9\u4e8e\u6bcf\u4e2a\u952e\u503c\u5bf9\uff0c\u518d\u4f7f\u7528split()\u65b9\u6cd5\u6309\u7b49\u53f7\u5206\u5272\u6210\u952e\u548c\u503c\u3002<\/li>\n<li>\u4f7f\u7528\u5b57\u5178\u89e3\u6790\u5c06\u8fd9\u4e9b\u952e\u503c\u5bf9\u8f6c\u6362\u4e3a\u5b57\u5178\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">def str_to_dict(s):<\/p>\n<p>    return dict(p<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>r.split(&#39;=&#39;) for pair in s.split(&#39;;&#39;))<\/p>\n<p>input_str = &quot;key1=value1;key2=value2&quot;<\/p>\n<p>result_dict = str_to_dict(input_str)<\/p>\n<p>print(result_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u65b9\u6cd5\u975e\u5e38\u7b80\u6d01\uff0c\u4f46\u8981\u6c42\u8f93\u5165\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u4e25\u683c\u9075\u5faa\u201ckey=value;key=value\u201d\u7684\u5f62\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/p>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u5904\u7406\u5b57\u7b26\u4e32\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u5c24\u5176\u9002\u5408\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5904\u7406\u4efb\u52a1\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u63d0\u53d6\u952e\u503c\u5bf9\uff0c\u7136\u540e\u8f6c\u6362\u4e3a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><p><strong>\u6b65\u9aa4<\/strong>\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u201ckey=value\u201d\u6a21\u5f0f\u7684\u5b57\u7b26\u4e32\u3002<\/li>\n<li>\u4f7f\u7528\u5b57\u5178\u89e3\u6790\u5c06\u8fd9\u4e9b\u5339\u914d\u7ed3\u679c\u8f6c\u6362\u4e3a\u5b57\u5178\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def str_to_dict(s):<\/p>\n<p>    pattern = re.compile(r&#39;(\\w+)=([^;]+)&#39;)<\/p>\n<p>    return {match.group(1): match.group(2) for match in pattern.finditer(s)}<\/p>\n<p>input_str = &quot;key1=value1;key2=value2&quot;<\/p>\n<p>result_dict = str_to_dict(input_str)<\/p>\n<p>print(result_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u66f4\u7075\u6d3b\uff0c\u53ef\u4ee5\u5904\u7406\u4e00\u4e9b\u590d\u6742\u7684\u5b57\u7b26\u4e32\uff0c\u4f46\u4ee3\u7801\u53ef\u80fd\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528csv\u6a21\u5757<\/p>\n<\/p>\n<p><p>csv\u6a21\u5757\u662fPython\u5185\u7f6e\u7684\u7528\u4e8e\u5904\u7406CSV\u6587\u4ef6\u7684\u6a21\u5757\uff0c\u4f46\u6211\u4eec\u4e5f\u53ef\u4ee5\u7528\u5b83\u6765\u5904\u7406\u8fd9\u79cd\u952e\u503c\u5bf9\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p><strong>\u6b65\u9aa4<\/strong>\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5c06\u8f93\u5165\u5b57\u7b26\u4e32\u7684\u5206\u9694\u7b26\u66ff\u6362\u4e3a\u9017\u53f7\u3002<\/li>\n<li>\u4f7f\u7528csv\u6a21\u5757\u7684DictReader\u6765\u8bfb\u53d6\u8fd9\u4e9b\u952e\u503c\u5bf9\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>import io<\/p>\n<p>def str_to_dict(s):<\/p>\n<p>    s = s.replace(&#39;;&#39;, &#39;,&#39;)<\/p>\n<p>    f = io.StringIO(s)<\/p>\n<p>    reader = csv.DictReader(f, fieldnames=[&#39;key&#39;, &#39;value&#39;], delimiter=&#39;=&#39;)<\/p>\n<p>    return {row[&#39;key&#39;]: row[&#39;value&#39;] for row in reader}<\/p>\n<p>input_str = &quot;key1=value1;key2=value2&quot;<\/p>\n<p>result_dict = str_to_dict(input_str)<\/p>\n<p>print(result_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u53ef\u80fd\u770b\u8d77\u6765\u6709\u4e9b\u590d\u6742\uff0c\u4f46\u5229\u7528csv\u6a21\u5757\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u5904\u7406\u4e00\u4e9b\u7279\u6b8a\u60c5\u51b5\uff0c\u6bd4\u5982\u952e\u503c\u5bf9\u4e2d\u5305\u542b\u5206\u9694\u7b26\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5c06\u7b49\u53f7\u8f6c\u6362\u4e3a\u5b57\u5178\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u548c\u8f93\u5165\u5b57\u7b26\u4e32\u7684\u590d\u6742\u6027\u3002<strong>\u4f7f\u7528\u5b57\u7b26\u4e32\u64cd\u4f5c\u548c\u5b57\u5178\u89e3\u6790<\/strong>\u65b9\u6cd5\u6700\u4e3a\u7b80\u5355\u76f4\u63a5\uff0c\u4f46\u8981\u6c42\u8f93\u5165\u683c\u5f0f\u4e25\u683c\u3002<strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\u65b9\u6cd5\u66f4\u4e3a\u7075\u6d3b\uff0c\u53ef\u4ee5\u5904\u7406\u590d\u6742\u7684\u5b57\u7b26\u4e32\u3002<strong>\u4f7f\u7528csv\u6a21\u5757<\/strong>\u65b9\u6cd5\u867d\u7136\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\uff0c\u4f46\u5728\u5904\u7406\u7279\u6b8a\u60c5\u51b5\u65f6\u975e\u5e38\u6709\u7528\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u5e76\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u7b49\u53f7\u8868\u8fbe\u5f0f\u8f6c\u6362\u4e3a\u5b57\u5178\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>split<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u6309\u7b49\u53f7\u62c6\u5206\uff0c\u5e76\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a\u5b57\u5178\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u5b57\u7b26\u4e32\u201ckey1=value1,key2=value2\u201d\uff0c\u53ef\u4ee5\u5148\u7528\u9017\u53f7\u5206\u5272\uff0c\u518d\u7528\u7b49\u53f7\u5206\u5272\u6bcf\u4e2a\u952e\u503c\u5bf9\uff0c\u6700\u540e\u751f\u6210\u5b57\u5178\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">input_string = &quot;key1=value1,key2=value2&quot;\ndict_result = dict(item.split(&quot;=&quot;) for item in input_string.split(&quot;,&quot;))\nprint(dict_result)  # \u8f93\u51fa: {&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;}\n<\/code><\/pre>\n<p><strong>\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\u53ef\u4ee5\u5c06\u7b49\u53f7\u8fde\u63a5\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u5178\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>split<\/code>\u65b9\u6cd5\uff0c\u8fd8\u53ef\u4ee5\u5229\u7528<code>json<\/code>\u6a21\u5757\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u683c\u5f0f\u7b26\u5408JSON\u7684\u8981\u6c42\u3002\u6bd4\u5982\u5b57\u7b26\u4e32\u201c{&quot;key1&quot;: &quot;value1&quot;, &quot;key2&quot;: &quot;value2&quot;}\u201d\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>json.loads()<\/code>\u65b9\u6cd5\u8fdb\u884c\u8f6c\u6362\uff1a<\/p>\n<pre><code class=\"language-python\">import json\n\njson_string = &#39;{&quot;key1&quot;: &quot;value1&quot;, &quot;key2&quot;: &quot;value2&quot;}&#39;\ndict_result = json.loads(json_string)\nprint(dict_result)  # \u8f93\u51fa: {&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;}\n<\/code><\/pre>\n<p><strong>\u5728\u5904\u7406\u542b\u6709\u7a7a\u683c\u6216\u7279\u6b8a\u5b57\u7b26\u7684\u952e\u503c\u5bf9\u65f6\uff0c\u5982\u4f55\u786e\u4fdd\u8f6c\u6362\u4e3a\u5b57\u5178\u65f6\u7684\u51c6\u786e\u6027\uff1f<\/strong><br \/>\u5728\u5904\u7406\u542b\u6709\u7a7a\u683c\u6216\u7279\u6b8a\u5b57\u7b26\u7684\u952e\u503c\u5bf9\u65f6\uff0c\u53ef\u4ee5\u5728\u62c6\u5206\u5b57\u7b26\u4e32\u4e4b\u524d\uff0c\u5148\u4f7f\u7528<code>strip<\/code>\u65b9\u6cd5\u53bb\u9664\u591a\u4f59\u7684\u7a7a\u683c\u3002\u786e\u4fdd\u952e\u548c\u503c\u90fd\u7ecf\u8fc7\u6e05\u7406\uff0c\u907f\u514d\u56e0\u683c\u5f0f\u95ee\u9898\u5bfc\u81f4\u7684\u8f6c\u6362\u9519\u8bef\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">input_string = &quot; key1 = value1 , key2 = value2 &quot;\ndict_result = dict(item.split(&quot;=&quot;) for item in input_string.split(&quot;,&quot;))\ndict_result = {key.strip(): value.strip() for key, value in dict_result.items()}\nprint(dict_result)  # \u8f93\u51fa: {&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u5c06\u7b49\u53f7\u8f6c\u6362\u4e3a\u5b57\u5178\u7684\u6838\u5fc3\u65b9\u6cd5\u662f\uff1a\u4f7f\u7528\u5b57\u7b26\u4e32\u64cd\u4f5c\u548c\u5b57\u5178\u89e3\u6790\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u4f7f\u7528csv\u6a21\u5757\u3002\u5176\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1077715,"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\/1077698"}],"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=1077698"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1077698\/revisions"}],"predecessor-version":[{"id":1077720,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1077698\/revisions\/1077720"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1077715"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1077698"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1077698"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1077698"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}