{"id":980679,"date":"2024-12-27T06:56:22","date_gmt":"2024-12-26T22:56:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/980679.html"},"modified":"2024-12-27T06:56:24","modified_gmt":"2024-12-26T22:56:24","slug":"python%e5%a6%82%e4%bd%95split%e5%a4%9a%e4%b8%aa%e7%ac%a6%e5%8f%b7","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/980679.html","title":{"rendered":"python\u5982\u4f55split\u591a\u4e2a\u7b26\u53f7"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24205954\/bec6ba20-965f-4e61-9c0e-81dc63a797fd.webp\" alt=\"python\u5982\u4f55split\u591a\u4e2a\u7b26\u53f7\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>re.split()<\/code>\u65b9\u6cd5\u6765\u5206\u5272\u591a\u4e2a\u7b26\u53f7\u3001\u4f7f\u7528<code>str.replace()<\/code>\u65b9\u6cd5\u9884\u5904\u7406\u5b57\u7b26\u4e32\u3001\u901a\u8fc7\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u8fdb\u884c\u591a\u6b21\u5206\u5272\u3002<\/strong>\u5176\u4e2d\uff0c<code>re.split()<\/code>\u65b9\u6cd5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u80fd\u591f\u4e00\u6b21\u6027\u5339\u914d\u591a\u4e2a\u7b26\u53f7\u8fdb\u884c\u5206\u5272\uff0c\u7075\u6d3b\u4e14\u5f3a\u5927\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u7684\u4f7f\u7528\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u4f7f\u7528 <code>re.split()<\/code> \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>Python\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5757<code>re<\/code>\u63d0\u4f9b\u4e86<code>re.split()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u591a\u4e2a\u5206\u9694\u7b26\u6765\u5206\u5272\u5b57\u7b26\u4e32\u3002<code>re.split()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u6765\u5206\u5272\u5b57\u7b26\u4e32\uff0c\u8fd9\u4f7f\u5f97\u5b83\u7279\u522b\u9002\u5408\u7528\u4e8e\u5206\u5272\u591a\u4e2a\u4e0d\u540c\u7b26\u53f7\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u8981\u4f7f\u7528<code>re.split()<\/code>\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165<code>re<\/code>\u6a21\u5757\u3002<code>re.split()<\/code>\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1a\u4e00\u4e2a\u662f\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff0c\u53e6\u4e00\u4e2a\u662f\u5f85\u5206\u5272\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>string = &quot;apple,banana;orange|grape&quot;<\/p>\n<p>delimiters = r&#39;[;,|]&#39;<\/p>\n<p>result = re.split(delimiters, string)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a[&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;, &#39;grape&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>[;,|]<\/code>\u8868\u793a\u4ee5\u9017\u53f7\u3001\u5206\u53f7\u6216\u7ad6\u7ebf\u4f5c\u4e3a\u5206\u9694\u7b26\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u590d\u6742\u7b26\u53f7<\/h4>\n<\/p>\n<p><p><code>re.split()<\/code>\u4e0d\u4ec5\u9650\u4e8e\u7b80\u5355\u7684\u7b26\u53f7\uff0c\u8fd8\u53ef\u4ee5\u5904\u7406\u590d\u6742\u7684\u5206\u9694\u7b26\uff0c\u4f8b\u5982\u7a7a\u767d\u5b57\u7b26\u3001\u6362\u884c\u7b26\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;apple, banana; orange\\tgrape\\nlemon&quot;<\/p>\n<p>delimiters = r&#39;[,\\s;]+&#39;<\/p>\n<p>result = re.split(delimiters, string)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a[&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;, &#39;grape&#39;, &#39;lemon&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u4f7f\u7528\u4e86<code>\\s<\/code>\u6765\u5339\u914d\u6240\u6709\u7a7a\u767d\u5b57\u7b26\uff0c\u5e76\u7528<code>+<\/code>\u6765\u8868\u793a\u4e00\u4e2a\u6216\u591a\u4e2a\u8fde\u7eed\u7684\u7a7a\u767d\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 <code>str.replace()<\/code> \u65b9\u6cd5\u9884\u5904\u7406\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<code>str.replace()<\/code>\u65b9\u6cd5\u5c06\u6240\u6709\u4e0d\u540c\u7684\u5206\u9694\u7b26\u66ff\u6362\u4e3a\u4e00\u4e2a\u7edf\u4e00\u7684\u5206\u9694\u7b26\uff0c\u7136\u540e\u4f7f\u7528<code>str.split()<\/code>\u8fdb\u884c\u5206\u5272\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u5c24\u5176\u9002\u5408\u5904\u7406\u5c11\u91cf\u7684\u5206\u9694\u7b26\u3002<\/p>\n<\/p>\n<p><h4>1. \u66ff\u6362\u5206\u9694\u7b26<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;apple,banana;orange|grape&quot;<\/p>\n<p>string = string.replace(&#39;,&#39;, &#39; &#39;).replace(&#39;;&#39;, &#39; &#39;).replace(&#39;|&#39;, &#39; &#39;)<\/p>\n<p>result = string.split()<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a[&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;, &#39;grape&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u9017\u53f7\u3001\u5206\u53f7\u548c\u7ad6\u7ebf\u5168\u90e8\u66ff\u6362\u4e3a\u7a7a\u683c\uff0c\u7136\u540e\u4f7f\u7528<code>split()<\/code>\u65b9\u6cd5\u5206\u5272\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f18\u5316\u66ff\u6362\u8fc7\u7a0b<\/h4>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u66ff\u6362\u7684\u5206\u9694\u7b26\u8f83\u591a\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u6216\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u4f18\u5316\u66ff\u6362\u8fc7\u7a0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">delimiters = [&#39;,&#39;, &#39;;&#39;, &#39;|&#39;]<\/p>\n<p>for delimiter in delimiters:<\/p>\n<p>    string = string.replace(delimiter, &#39; &#39;)<\/p>\n<p>result = string.split()<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a[&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;, &#39;grape&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u901a\u8fc7\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u8fdb\u884c\u591a\u6b21\u5206\u5272<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e0d\u60f3\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7528\u6237\uff0c\u53ef\u4ee5\u901a\u8fc7\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u5206\u591a\u6b65\u8fdb\u884c\u5206\u5272\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u4f9d\u6b21\u5904\u7406\u4e0d\u540c\u5206\u9694\u7b26\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>1. \u5206\u6b65\u5206\u5272<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;apple,banana;orange|grape&quot;<\/p>\n<p>delimiters = [&#39;,&#39;, &#39;;&#39;, &#39;|&#39;]<\/p>\n<p>for delimiter in delimiters:<\/p>\n<p>    string = &#39; &#39;.join(string.split(delimiter))<\/p>\n<p>result = string.split()<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a[&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;, &#39;grape&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>split(delimiter)<\/code>\u9010\u6b65\u66ff\u6362\u6bcf\u4e00\u4e2a\u5206\u9694\u7b26\u4e3a\u7edf\u4e00\u7684\u7a7a\u683c\uff0c\u6700\u540e\u518d\u8fdb\u884c\u5206\u5272\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u7075\u6d3b\u4f7f\u7528<code>re.split()<\/code>\u3001<code>str.replace()<\/code>\u3001\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u7b49\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u591a\u79cd\u5206\u9694\u7b26\u7684\u5b57\u7b26\u4e32\u5206\u5272\u3002<\/strong>\u5176\u4e2d\uff0c<code>re.split()<\/code>\u6700\u4e3a\u5f3a\u5927\uff0c\u9002\u5408\u590d\u6742\u5206\u9694\u7b26\u7684\u60c5\u51b5\uff1b<code>str.replace()<\/code>\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u5408\u5c11\u91cf\u5206\u9694\u7b26\uff1b\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u7075\u6d3b\u9002\u7528\uff0c\u9002\u5408\u4f9d\u6b21\u5904\u7406\u7684\u60c5\u51b5\u3002\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528split\u65b9\u6cd5\u5904\u7406\u591a\u4e2a\u5206\u9694\u7b26\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0csplit\u65b9\u6cd5\u9ed8\u8ba4\u53ea\u63a5\u53d7\u4e00\u4e2a\u5206\u9694\u7b26\u3002\u5982\u679c\u9700\u8981\u5904\u7406\u591a\u4e2a\u5206\u9694\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u901a\u8fc7\u5bfc\u5165re\u6a21\u5757\uff0c\u5229\u7528re.split()\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>re.split(r&#39;[;,\\s]+&#39;, string)<\/code>\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u6309\u7167\u5206\u53f7\u3001\u9017\u53f7\u548c\u7a7a\u683c\u8fdb\u884c\u5206\u5272\u3002<\/p>\n<p><strong>\u4f7f\u7528split\u65b9\u6cd5\u65f6\uff0c\u6709\u4ec0\u4e48\u6280\u5de7\u53ef\u4ee5\u63d0\u9ad8\u6548\u7387\uff1f<\/strong><br \/>\u5728\u5904\u7406\u5927\u578b\u5b57\u7b26\u4e32\u65f6\uff0c\u4f7f\u7528split\u65b9\u6cd5\u65f6\u53ef\u4ee5\u8003\u8651\u76f4\u63a5\u5728\u5b57\u7b26\u4e32\u4e0a\u8c03\u7528\u8be5\u65b9\u6cd5\uff0c\u8fd9\u6837\u53ef\u4ee5\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\u3002\u540c\u65f6\uff0c\u5982\u679c\u9700\u8981\u9891\u7e41\u8fdb\u884c\u5206\u5272\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5c06\u5206\u9694\u7b26\u7f16\u8bd1\u6210\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u63d0\u9ad8\u6027\u80fd\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u53ea\u5bf9\u5fc5\u8981\u7684\u90e8\u5206\u8fdb\u884c\u5206\u5272\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u907f\u514dsplit\u65b9\u6cd5\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5982\u679c\u5e0c\u671b\u5728\u4f7f\u7528split\u65f6\u907f\u514d\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u5728\u5206\u5272\u540e\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u8fc7\u6ee4\u6389\u7a7a\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c<code>[s for s in string.split(&#39;,&#39;) if s]<\/code>\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6ca1\u6709\u7a7a\u5b57\u7b26\u4e32\u7684\u5217\u8868\u3002\u8fd9\u79cd\u65b9\u5f0f\u786e\u4fdd\u4e86\u7ed3\u679c\u66f4\u52a0\u5e72\u51c0\u548c\u6709\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528re.split()\u65b9\u6cd5\u6765\u5206\u5272\u591a\u4e2a\u7b26\u53f7\u3001\u4f7f\u7528str.replace()\u65b9\u6cd5\u9884\u5904\u7406\u5b57\u7b26 [&hellip;]","protected":false},"author":3,"featured_media":980685,"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\/980679"}],"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=980679"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/980679\/revisions"}],"predecessor-version":[{"id":980688,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/980679\/revisions\/980688"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/980685"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=980679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=980679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=980679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}