{"id":1056964,"date":"2024-12-31T15:03:55","date_gmt":"2024-12-31T07:03:55","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1056964.html"},"modified":"2024-12-31T15:03:57","modified_gmt":"2024-12-31T07:03:57","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%88%86%e5%bc%80","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1056964.html","title":{"rendered":"python\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u5206\u5f00"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/5b952239-fb97-4da0-867a-ed5a63f32a27.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u5206\u5f00\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5c06\u5b57\u7b26\u4e32\u5206\u5f00\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528split()\u65b9\u6cd5\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001\u4f7f\u7528\u5207\u7247\u7b49\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u901a\u8fc7\u793a\u4f8b\u6765\u6f14\u793a\u6bcf\u79cd\u65b9\u6cd5\u7684\u5177\u4f53\u4f7f\u7528\u65b9\u5f0f\u3002<\/strong><\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528split()\u65b9\u6cd5<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p><code>split()<\/code>\u65b9\u6cd5\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u5b57\u7b26\u4e32\u5206\u5272\u65b9\u6cd5\u3002\u5b83\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u6309\u7167\u6307\u5b9a\u7684\u5206\u9694\u7b26\u8fdb\u884c\u5206\u5272\uff0c\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, how are you?&quot;<\/p>\n<p>words = text.split()<\/p>\n<p>print(words)  # \u8f93\u51fa: [&#39;Hello,&#39;, &#39;how&#39;, &#39;are&#39;, &#39;you?&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>split()<\/code>\u65b9\u6cd5\u4f7f\u7528\u9ed8\u8ba4\u7684\u7a7a\u683c\u4f5c\u4e3a\u5206\u9694\u7b26\uff0c\u5c06\u5b57\u7b26\u4e32<code>text<\/code>\u5206\u5272\u6210\u4e86\u4e00\u4e2a\u5355\u8bcd\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>2. \u6307\u5b9a\u5206\u9694\u7b26<\/h3>\n<\/p>\n<p><p><code>split()<\/code>\u65b9\u6cd5\u8fd8\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff0c\u7528\u4e8e\u6307\u5b9a\u5206\u9694\u7b26\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u9017\u53f7\u6765\u5206\u5272\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;apple,banana,orange&quot;<\/p>\n<p>fruits = text.split(&#39;,&#39;)<\/p>\n<p>print(fruits)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u6307\u5b9a\u5206\u5272\u6b21\u6570<\/h3>\n<\/p>\n<p><p><code>split()<\/code>\u65b9\u6cd5\u8fd8\u53ef\u4ee5\u63a5\u53d7\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u6307\u5b9a\u6700\u591a\u5206\u5272\u7684\u6b21\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;apple-banana-orange&quot;<\/p>\n<p>fruits = text.split(&#39;-&#39;, 1)<\/p>\n<p>print(fruits)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana-orange&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5b57\u7b26\u4e32\u53ea\u8fdb\u884c\u4e86\u7b2c\u4e00\u6b21\u5206\u5272\uff0c\u5269\u4e0b\u7684\u5b57\u7b26\u4e32\u4fdd\u6301\u4e0d\u53d8\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u4f9b\u4e86\u66f4\u4e3a\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u5206\u5272\u529f\u80fd\uff0c\u7279\u522b\u9002\u7528\u4e8e\u590d\u6742\u7684\u5206\u5272\u9700\u6c42\u3002Python\u7684<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86<code>re.split()<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;one1two2three3four&quot;<\/p>\n<p>words = re.split(r&#39;\\d&#39;, text)<\/p>\n<p>print(words)  # \u8f93\u51fa: [&#39;one&#39;, &#39;two&#39;, &#39;three&#39;, &#39;four&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f<code>\\d<\/code>\u6765\u5339\u914d\u6570\u5b57\uff0c\u5c06\u5b57\u7b26\u4e32\u6309\u7167\u6570\u5b57\u8fdb\u884c\u5206\u5272\u3002<\/p>\n<\/p>\n<p><h3>2. \u4f7f\u7528\u591a\u4e2a\u5206\u9694\u7b26<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u8fd8\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5206\u9694\u7b26\u3002\u4f8b\u5982\uff0c\u5206\u5272\u5b57\u7b26\u4e32\u65f6\u53ef\u4ee5\u540c\u65f6\u4f7f\u7528\u9017\u53f7\u548c\u7a7a\u683c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;apple, banana orange&quot;<\/p>\n<p>fruits = re.split(r&#39;[,\\s]+&#39;, text)<\/p>\n<p>print(fruits)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f<code>[,\\s]+<\/code>\uff0c\u8868\u793a\u5339\u914d\u9017\u53f7\u6216\u7a7a\u683c\uff0c\u5c06\u5b57\u7b26\u4e32\u8fdb\u884c\u5206\u5272\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7ed3\u5408<code>split()<\/code>\u65b9\u6cd5\u6765\u5206\u5272\u5b57\u7b26\u4e32\u5e76\u8fdb\u4e00\u6b65\u5904\u7406\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;apple,banana,orange&quot;<\/p>\n<p>fruits = [fruit.strip() for fruit in text.split(&#39;,&#39;)]<\/p>\n<p>print(fruits)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528\u5217\u8868\u89e3\u6790\u5bf9\u6bcf\u4e2a\u5206\u5272\u540e\u7684\u5b57\u7b26\u4e32\u8fdb\u884c\u4e86<code>strip()<\/code>\u64cd\u4f5c\uff0c\u53bb\u9664\u4e86\u591a\u4f59\u7684\u7a7a\u683c\u3002<\/p>\n<\/p>\n<p><h3>2. \u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u8fd8\u53ef\u4ee5\u7ed3\u5408\u5176\u4ed6\u5b57\u7b26\u4e32\u65b9\u6cd5\u6765\u8fdb\u884c\u66f4\u590d\u6742\u7684\u5904\u7406\u3002\u4f8b\u5982\uff0c\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5355\u8bcd\u8f6c\u6362\u4e3a\u5927\u5199\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;apple, banana, orange&quot;<\/p>\n<p>fruits = [fruit.strip().upper() for fruit in text.split(&#39;,&#39;)]<\/p>\n<p>print(fruits)  # \u8f93\u51fa: [&#39;APPLE&#39;, &#39;BANANA&#39;, &#39;ORANGE&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528\u5207\u7247<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u5207\u7247\u662f\u4e00\u79cd\u66f4\u4e3a\u5e95\u5c42\u7684\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5df2\u77e5\u56fa\u5b9a\u6a21\u5f0f\u7684\u5b57\u7b26\u4e32\u5206\u5272\u3002\u4f8b\u5982\uff0c\u5206\u5272\u56fa\u5b9a\u957f\u5ea6\u7684\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;abcdef&quot;<\/p>\n<p>part1 = text[:3]<\/p>\n<p>part2 = text[3:]<\/p>\n<p>print(part1, part2)  # \u8f93\u51fa: abc def<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u5c06\u5b57\u7b26\u4e32<code>text<\/code>\u5206\u5272\u6210\u4e86\u4e24\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><h3>2. \u5206\u5272\u6210\u591a\u4e2a\u90e8\u5206<\/h3>\n<\/p>\n<p><p>\u5207\u7247\u8fd8\u53ef\u4ee5\u7ed3\u5408\u5faa\u73af\uff0c\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u591a\u4e2a\u56fa\u5b9a\u957f\u5ea6\u7684\u90e8\u5206\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;abcdefghi&quot;<\/p>\n<p>parts = [text[i:i+3] for i in range(0, len(text), 3)]<\/p>\n<p>print(parts)  # \u8f93\u51fa: [&#39;abc&#39;, &#39;def&#39;, &#39;ghi&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528\u4e86\u5217\u8868\u89e3\u6790\u548c\u5207\u7247\u64cd\u4f5c\uff0c\u5c06\u5b57\u7b26\u4e32<code>text<\/code>\u5206\u5272\u6210\u4e86\u591a\u4e2a\u957f\u5ea6\u4e3a3\u7684\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u4f7f\u7528partition()\u65b9\u6cd5<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p><code>partition()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u4e3a\u4e09\u90e8\u5206\uff1a\u5206\u9694\u7b26\u524d\u7684\u90e8\u5206\u3001\u5206\u9694\u7b26\u672c\u8eab\u3001\u5206\u9694\u7b26\u540e\u7684\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;apple-banana-orange&quot;<\/p>\n<p>head, sep, t<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l = text.partition(&#39;-&#39;)<\/p>\n<p>print(head, sep, tail)  # \u8f93\u51fa: apple - banana-orange<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>partition()<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u4e3a\u4e86\u4e09\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><h3>2. \u7ed3\u5408\u5faa\u73af\u4f7f\u7528<\/h3>\n<\/p>\n<p><p><code>partition()<\/code>\u65b9\u6cd5\u8fd8\u53ef\u4ee5\u7ed3\u5408\u5faa\u73af\uff0c\u9010\u6b65\u5206\u5272\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;apple-banana-orange&quot;<\/p>\n<p>while &#39;-&#39; in text:<\/p>\n<p>    head, sep, text = text.partition(&#39;-&#39;)<\/p>\n<p>    print(head)<\/p>\n<p>print(text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528\u5faa\u73af\u9010\u6b65\u5206\u5272\u5b57\u7b26\u4e32\uff0c\u76f4\u5230\u6ca1\u6709\u5206\u9694\u7b26\u4e3a\u6b62\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u4f7f\u7528rsplit()\u65b9\u6cd5<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p><code>rsplit()<\/code>\u65b9\u6cd5\u4e0e<code>split()<\/code>\u65b9\u6cd5\u7c7b\u4f3c\uff0c\u4f46\u4ece\u5b57\u7b26\u4e32\u7684\u53f3\u4fa7\u5f00\u59cb\u5206\u5272\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;apple-banana-orange&quot;<\/p>\n<p>fruits = text.rsplit(&#39;-&#39;, 1)<\/p>\n<p>print(fruits)  # \u8f93\u51fa: [&#39;apple-banana&#39;, &#39;orange&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>rsplit()<\/code>\u65b9\u6cd5\u4ece\u53f3\u4fa7\u5f00\u59cb\u5206\u5272\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>2. \u7ed3\u5408\u5176\u4ed6\u5b57\u7b26\u4e32\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>rsplit()<\/code>\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u7ed3\u5408\u5176\u4ed6\u5b57\u7b26\u4e32\u65b9\u6cd5\u8fdb\u884c\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;apple, banana, orange&quot;<\/p>\n<p>fruits = [fruit.strip() for fruit in text.rsplit(&#39;,&#39;, 1)]<\/p>\n<p>print(fruits)  # \u8f93\u51fa: [&#39;apple, banana&#39;, &#39;orange&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u7ed3\u5408\u4e86<code>strip()<\/code>\u65b9\u6cd5\u53bb\u9664\u591a\u4f59\u7684\u7a7a\u683c\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u4f7f\u7528csv\u6a21\u5757<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p><code>csv<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5bf9\u9017\u53f7\u5206\u9694\u503c\uff08CSV\uff09\u6587\u4ef6\u7684\u8bfb\u5199\u529f\u80fd\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5206\u5272\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>import io<\/p>\n<p>text = &quot;apple,banana,orange&quot;<\/p>\n<p>f = io.StringIO(text)<\/p>\n<p>reader = csv.reader(f)<\/p>\n<p>for row in reader:<\/p>\n<p>    print(row)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528<code>csv.reader()<\/code>\u65b9\u6cd5\u8bfb\u53d6\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u5176\u5206\u5272\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>2. \u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><p><code>csv<\/code>\u6a21\u5757\u8fd8\u53ef\u4ee5\u5904\u7406\u66f4\u590d\u6742\u7684CSV\u683c\u5f0f\uff0c\u4f8b\u5982\u5e26\u6709\u5f15\u53f7\u7684\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>import io<\/p>\n<p>text = &#39;apple,&quot;banana, orange&quot;,grape&#39;<\/p>\n<p>f = io.StringIO(text)<\/p>\n<p>reader = csv.reader(f)<\/p>\n<p>for row in reader:<\/p>\n<p>    print(row)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana, orange&#39;, &#39;grape&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>csv.reader()<\/code>\u65b9\u6cd5\u6b63\u786e\u5904\u7406\u4e86\u5e26\u6709\u5f15\u53f7\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h2>\u516b\u3001\u4f7f\u7528str.extract()\u65b9\u6cd5<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p><code>str.extract()<\/code>\u65b9\u6cd5\u662fPandas\u5e93\u4e2d\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>text = &quot;apple,banana,orange&quot;<\/p>\n<p>df = pd.DataFrame({&#39;text&#39;: [text]})<\/p>\n<p>df[&#39;fruits&#39;] = df[&#39;text&#39;].str.split(&#39;,&#39;)<\/p>\n<p>print(df[&#39;fruits&#39;].iloc[0])  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528Pandas\u5e93\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>2. \u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><p><code>str.extract()<\/code>\u65b9\u6cd5\u8fd8\u53ef\u4ee5\u7ed3\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u7279\u5b9a\u6a21\u5f0f\u7684\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>text = &quot;apple, banana, orange&quot;<\/p>\n<p>df = pd.DataFrame({&#39;text&#39;: [text]})<\/p>\n<p>df[[&#39;fruit1&#39;, &#39;fruit2&#39;, &#39;fruit3&#39;]] = df[&#39;text&#39;].str.extract(r&#39;([^,]+),([^,]+),([^,]+)&#39;)<\/p>\n<p>print(df)  # \u8f93\u51fa:   text   fruit1  fruit2  fruit3<\/p>\n<p>           #       apple, banana, orange  apple  banana  orange<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u5185\u5bb9\u63d0\u53d6\u5230\u4e0d\u540c\u7684\u5217\u4e2d\u3002<\/p>\n<\/p>\n<p><h2>\u4e5d\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u8be6\u7ec6\u8bb2\u89e3\u4e86Python\u4e2d\u591a\u79cd\u5c06\u5b57\u7b26\u4e32\u5206\u5f00\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528<code>split()<\/code>\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5217\u8868\u89e3\u6790\u3001\u5207\u7247\u3001<code>partition()<\/code>\u65b9\u6cd5\u3001<code>rsplit()<\/code>\u65b9\u6cd5\u3001<code>csv<\/code>\u6a21\u5757\u3001\u4ee5\u53caPandas\u5e93\u4e2d\u7684<code>str.extract()<\/code>\u65b9\u6cd5\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u573a\u666f\uff0c\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5b8c\u6210\u5b57\u7b26\u4e32\u5206\u5272\u4efb\u52a1\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\u6309\u7279\u5b9a\u5b57\u7b26\u5206\u5f00\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>split()<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u6309\u7279\u5b9a\u5b57\u7b26\u5206\u5f00\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u60f3\u6309\u7a7a\u683c\u5206\u5f00\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<code>my_string.split(&#39; &#39;)<\/code>\u3002\u5982\u679c\u60f3\u6309\u5176\u4ed6\u5b57\u7b26\u5206\u5f00\uff0c\u6bd4\u5982\u9017\u53f7\uff0c\u53ef\u4ee5\u4f7f\u7528<code>my_string.split(&#39;,&#39;)<\/code>\u3002\u6b64\u65b9\u6cd5\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\uff0c\u5305\u542b\u5206\u5f00\u7684\u5b57\u7b26\u4e32\u90e8\u5206\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u5206\u5f00\u7684\u5b57\u7b26\u4e32\u5217\u8868\uff1f<\/strong><br \/>\u5904\u7406\u5206\u5f00\u7684\u5b57\u7b26\u4e32\u5217\u8868\u975e\u5e38\u7b80\u5355\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u7279\u5b9a\u7d22\u5f15\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>for item in my_list:<\/code>\u53ef\u4ee5\u904d\u5386\u5217\u8868\u3002\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>my_list[index]<\/code>\u76f4\u63a5\u8bbf\u95ee\u7279\u5b9a\u5143\u7d20\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5904\u7406\u548c\u64cd\u4f5c\u5206\u5f00\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5206\u5272\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u66f4\u590d\u6742\u7684\u5206\u5272\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>re<\/code>\u6a21\u5757\u4e2d\u7684<code>re.split()<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u5141\u8bb8\u60a8\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5b9a\u4e49\u5206\u9694\u7b26\u3002\u4f8b\u5982\uff0c<code>re.split(r&#39;\\W+&#39;, my_string)<\/code>\u53ef\u4ee5\u6839\u636e\u975e\u5b57\u6bcd\u6570\u5b57\u5b57\u7b26\u5c06\u5b57\u7b26\u4e32\u5206\u5f00\u3002\u8fd9\u79cd\u65b9\u6cd5\u63d0\u4f9b\u4e86\u66f4\u591a\u7075\u6d3b\u6027\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5206\u5272\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5c06\u5b57\u7b26\u4e32\u5206\u5f00\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528split()\u65b9\u6cd5\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790 [&hellip;]","protected":false},"author":3,"featured_media":1056974,"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\/1056964"}],"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=1056964"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1056964\/revisions"}],"predecessor-version":[{"id":1056975,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1056964\/revisions\/1056975"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1056974"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1056964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1056964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1056964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}