{"id":1144442,"date":"2025-01-08T22:59:45","date_gmt":"2025-01-08T14:59:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1144442.html"},"modified":"2025-01-08T22:59:47","modified_gmt":"2025-01-08T14:59:47","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e4%b8%80%e6%ae%b5%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%bd%ac%e5%8c%96%e4%b8%ba%e5%88%97%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1144442.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u4e00\u6bb5\u5b57\u7b26\u4e32\u8f6c\u5316\u4e3a\u5217\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181540\/05072e98-2c8a-4f8d-a3bc-fdc261952a8e.webp\" alt=\"python\u4e2d\u5982\u4f55\u4e00\u6bb5\u5b57\u7b26\u4e32\u8f6c\u5316\u4e3a\u5217\u8868\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5c06\u4e00\u6bb5\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u4e3b\u8981\u7684\u65b9\u5f0f\u5305\u62ec\uff1a\u4f7f\u7528split()\u65b9\u6cd5\u3001list()\u6784\u9020\u51fd\u6570\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001ast.literal_eval()\u7b49\u3002<\/strong>\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u8ba8\u8bba\u5176\u4e2d\u7684\u4e00\u79cd\u5e38\u7528\u65b9\u6cd5\uff0c\u5373\u4f7f\u7528split()\u65b9\u6cd5\uff0c\u5e76\u7b80\u8981\u4ecb\u7ecd\u5176\u4ed6\u65b9\u6cd5\u7684\u4f7f\u7528\u573a\u666f\u548c\u5177\u4f53\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528split()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>split()\u65b9\u6cd5\u662f\u5c06\u5b57\u7b26\u4e32\u6309\u7167\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5207\u5272\u6210\u591a\u4e2a\u5b50\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u8fd9\u4e9b\u5b50\u5b57\u7b26\u4e32\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u3002\u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u5e38\u7528\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u9ed8\u8ba4\u5206\u9694\u7b26<\/h4>\n<\/p>\n<p><p>\u5f53\u4e0d\u6307\u5b9a\u5206\u9694\u7b26\u65f6\uff0csplit()\u65b9\u6cd5\u4f1a\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u767d\u5b57\u7b26\uff08\u5305\u62ec\u7a7a\u683c\u3001\u5236\u8868\u7b26\u3001\u6362\u884c\u7b26\u7b49\uff09\u4f5c\u4e3a\u5206\u9694\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Python is a powerful programming language&quot;<\/p>\n<p>result = text.split()<\/p>\n<p>print(result)  # \u8f93\u51fa: [&#39;Python&#39;, &#39;is&#39;, &#39;a&#39;, &#39;powerful&#39;, &#39;programming&#39;, &#39;language&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5355\u8bcd\u88ab\u4f5c\u4e3a\u5217\u8868\u4e2d\u7684\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u6307\u5b9a\u7684\u5206\u9694\u7b26<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4f20\u5165\u4e00\u4e2a\u5b57\u7b26\u4e32\u53c2\u6570\u6765\u6307\u5b9a\u5206\u9694\u7b26\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u9017\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Python,is,a,powerful,programming,language&quot;<\/p>\n<p>result = text.split(&#39;,&#39;)<\/p>\n<p>print(result)  # \u8f93\u51fa: [&#39;Python&#39;, &#39;is&#39;, &#39;a&#39;, &#39;powerful&#39;, &#39;programming&#39;, &#39;language&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u9002\u5408\u5904\u7406\u4ee5\u7279\u5b9a\u5b57\u7b26\u5206\u9694\u7684\u5b57\u7b26\u4e32\uff0c\u5982CSV\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528list()\u6784\u9020\u51fd\u6570<\/h3>\n<\/p>\n<p><p>list()\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u4f5c\u4e3a\u5217\u8868\u4e2d\u7684\u4e00\u4e2a\u5143\u7d20\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u9010\u5b57\u7b26\u5904\u7406\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Python&quot;<\/p>\n<p>result = list(text)<\/p>\n<p>print(result)  # \u8f93\u51fa: [&#39;P&#39;, &#39;y&#39;, &#39;t&#39;, &#39;h&#39;, &#39;o&#39;, &#39;n&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u5e38\u7528\u4e8e\u4e00\u822c\u7684\u5b57\u7b26\u4e32\u5904\u7406\uff0c\u4f46\u5728\u67d0\u4e9b\u7279\u5b9a\u7684\u573a\u666f\u4e0b\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u5904\u7406\u80fd\u529b\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u590d\u6742\u7684\u5206\u9694\u7b26\u65f6\u975e\u5e38\u6709\u7528\u3002re\u6a21\u5757\u4e2d\u7684re.split()\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5206\u5272\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;Python,is a powerful; programming language&quot;<\/p>\n<p>result = re.split(r&#39;[ ,;]&#39;, text)<\/p>\n<p>print(result)  # \u8f93\u51fa: [&#39;Python&#39;, &#39;is&#39;, &#39;a&#39;, &#39;powerful&#39;, &#39;&#39;, &#39;programming&#39;, &#39;language&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f <code>[ ,;]<\/code> \u8868\u793a\u53ef\u4ee5\u7528\u7a7a\u683c\u3001\u9017\u53f7\u6216\u5206\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528ast.literal_eval()<\/h3>\n<\/p>\n<p><p>\u5f53\u5b57\u7b26\u4e32\u8868\u793a\u7684\u662f\u4e00\u4e2a\u5217\u8868\u7684\u6587\u672c\u5f62\u5f0f\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528ast.literal_eval()\u5c06\u5176\u8f6c\u6362\u4e3a\u771f\u6b63\u7684\u5217\u8868\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u5b57\u7b26\u4e32\u88ab\u5b89\u5168\u5730\u89e3\u6790\u4e3aPython\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ast<\/p>\n<p>text = &quot;[&#39;Python&#39;, &#39;is&#39;, &#39;a&#39;, &#39;powerful&#39;, &#39;programming&#39;, &#39;language&#39;]&quot;<\/p>\n<p>result = ast.literal_eval(text)<\/p>\n<p>print(result)  # \u8f93\u51fa: [&#39;Python&#39;, &#39;is&#39;, &#39;a&#39;, &#39;powerful&#39;, &#39;programming&#39;, &#39;language&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u9002\u7528\u4e8e\u4ece\u6587\u672c\u6587\u4ef6\u6216\u5176\u4ed6\u6570\u636e\u6e90\u4e2d\u8bfb\u53d6\u8868\u793a\u5217\u8868\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u3002<strong>split()\u65b9\u6cd5\u9002\u7528\u4e8e\u4e00\u822c\u7684\u5b57\u7b26\u4e32\u5206\u5272\u3001list()\u6784\u9020\u51fd\u6570\u9002\u7528\u4e8e\u9010\u5b57\u7b26\u5904\u7406\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u9002\u7528\u4e8e\u590d\u6742\u7684\u5206\u9694\u7b26\u3001ast.literal_eval()\u9002\u7528\u4e8e\u89e3\u6790\u8868\u793a\u5217\u8868\u7684\u5b57\u7b26\u4e32\u3002<\/strong>\u901a\u8fc7\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u5b57\u7b26\u4e32\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u6309\u7279\u5b9a\u5206\u9694\u7b26\u8f6c\u6362\u4e3a\u5217\u8868\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>split()<\/code>\u65b9\u6cd5\u6765\u6309\u7279\u5b9a\u7684\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u7528\u9017\u53f7\u5206\u9694\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<code>my_string = &quot;apple,banana,orange&quot;<\/code>\uff0c\u7136\u540e\u8c03\u7528<code>my_list = my_string.split(&quot;,&quot;)<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u5217\u8868<code>[&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;]<\/code>\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u6bcf\u4e2a\u5b57\u7b26\u8f6c\u6362\u4e3a\u5217\u8868\u4e2d\u7684\u5355\u72ec\u5143\u7d20\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528<code>list()<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u8f6c\u6362\u4e3a\u5217\u8868\u4e2d\u7684\u5355\u72ec\u5143\u7d20\u3002\u4f8b\u5982\uff0c<code>my_string = &quot;hello&quot;<\/code>\uff0c\u901a\u8fc7<code>my_list = list(my_string)<\/code>\uff0c\u4f60\u5c06\u5f97\u5230<code>[&#39;h&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;]<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u5305\u542b\u7a7a\u683c\u7684\u5b57\u7b26\u4e32\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\uff1f<\/strong><br \/>\u5bf9\u4e8e\u5305\u542b\u7a7a\u683c\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528<code>split()<\/code>\u65b9\u6cd5\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u5b83\u4f1a\u6839\u636e\u7a7a\u683c\u8fdb\u884c\u5206\u5272\u3002\u4f8b\u5982\uff0c<code>my_string = &quot;hello world python&quot;<\/code>\uff0c\u8c03\u7528<code>my_list = my_string.split()<\/code>\u5c06\u5f97\u5230<code>[&#39;hello&#39;, &#39;world&#39;, &#39;python&#39;]<\/code>\uff0c\u6709\u6548\u5730\u79fb\u9664\u4e86\u591a\u4f59\u7684\u7a7a\u683c\uff0c\u5e76\u751f\u6210\u4e86\u4e00\u4e2a\u5e72\u51c0\u7684\u5217\u8868\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5c06\u4e00\u6bb5\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u4e3b\u8981\u7684\u65b9\u5f0f\u5305\u62ec\uff1a\u4f7f\u7528split()\u65b9\u6cd5\u3001list()\u6784 [&hellip;]","protected":false},"author":3,"featured_media":1144446,"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\/1144442"}],"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=1144442"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1144442\/revisions"}],"predecessor-version":[{"id":1144447,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1144442\/revisions\/1144447"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1144446"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1144442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1144442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1144442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}