{"id":1110499,"date":"2025-01-08T17:20:17","date_gmt":"2025-01-08T09:20:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1110499.html"},"modified":"2025-01-08T17:20:19","modified_gmt":"2025-01-08T09:20:19","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86str%e8%bd%ac%e5%8c%96%e6%88%90array","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1110499.html","title":{"rendered":"python\u5982\u4f55\u5c06str\u8f6c\u5316\u6210array"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073324\/68c86d43-10e5-423b-bdfc-07a88a678b00.webp\" alt=\"python\u5982\u4f55\u5c06str\u8f6c\u5316\u6210array\" \/><\/p>\n<p><p> <strong>Python \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u5305\u62ec\u4f7f\u7528 <code>split()<\/code> \u65b9\u6cd5\u3001\u5217\u8868\u89e3\u6790\u3001\u4ee5\u53ca\u4f7f\u7528 <code>ast.literal_eval<\/code> \u6a21\u5757\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u6765\u4f7f\u7528<\/strong>\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u5e76\u901a\u8fc7\u5b9e\u4f8b\u89e3\u91ca\u5176\u5177\u4f53\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528 <code>split()<\/code> \u65b9\u6cd5<\/strong>\uff1a\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\u4e14\u5e38\u7528\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u7531\u7279\u5b9a\u5206\u9694\u7b26\u5206\u9694\u7684\u5b57\u7b26\u4e32\u65f6\u3002\u5b83\u80fd\u591f\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u6210\u5b50\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u8fd9\u4e9b\u5b50\u5b57\u7b26\u4e32\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;apple,banana,cherry&quot;<\/p>\n<p>array = string.split(&quot;,&quot;)<\/p>\n<p>print(array)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528 <code>split()<\/code> \u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u6570\u7ec4\u3002\u5047\u5982\u6211\u4eec\u6709\u4e00\u4e2a\u4ee5\u9017\u53f7\u5206\u9694\u7684\u5b57\u7b26\u4e32\uff0c\u901a\u8fc7\u8c03\u7528 <code>split(&quot;,&quot;)<\/code> \u65b9\u6cd5\uff0c\u6211\u4eec\u5c31\u80fd\u5c06\u5176\u8f6c\u6362\u6210\u4e00\u4e2a\u5305\u542b\u6bcf\u4e2a\u5b50\u5b57\u7b26\u4e32\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4ed6\u51e0\u79cd\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5176\u9002\u7528\u573a\u666f\u548c\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528 <code>split()<\/code> \u65b9\u6cd5<\/h2>\n<\/p>\n<p><p><code>split()<\/code> \u65b9\u6cd5\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u9002\u7528\u4e8e\u5904\u7406\u7531\u7279\u5b9a\u5206\u9694\u7b26\u5206\u9694\u7684\u5b57\u7b26\u4e32\u3002\u5b83\u53ef\u4ee5\u6839\u636e\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u6210\u5b50\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u8fd9\u4e9b\u5b50\u5b57\u7b26\u4e32\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;apple,banana,cherry&quot;<\/p>\n<p>array = string.split(&quot;,&quot;)<\/p>\n<p>print(array)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u4ee5\u9017\u53f7\u5206\u9694\u7684\u5b57\u7b26\u4e32\uff0c\u901a\u8fc7\u8c03\u7528 <code>split(&quot;,&quot;)<\/code> \u65b9\u6cd5\uff0c\u6211\u4eec\u5c06\u5176\u8f6c\u6362\u6210\u4e00\u4e2a\u5305\u542b\u6bcf\u4e2a\u5b50\u5b57\u7b26\u4e32\u7684\u5217\u8868\u3002<code>split()<\/code> \u65b9\u6cd5\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4f1a\u53bb\u6389\u5206\u9694\u7b26\uff0c\u5982\u679c\u4e0d\u6307\u5b9a\u5206\u9694\u7b26\uff0c\u9ed8\u8ba4\u4f1a\u4f7f\u7528\u7a7a\u683c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;apple banana cherry&quot;<\/p>\n<p>array = string.split()<\/p>\n<p>print(array)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/h2>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u975e\u5e38\u5f3a\u5927\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u9700\u6c42\u3002\u5b83\u53ef\u4ee5\u7ed3\u5408\u6761\u4ef6\u548c\u64cd\u4f5c\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u62c6\u5206\u548c\u5904\u7406\uff0c\u5e76\u5c06\u7ed3\u679c\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;1,2,3,4,5&quot;<\/p>\n<p>array = [int(x) for x in string.split(&quot;,&quot;)]<\/p>\n<p>print(array)  # \u8f93\u51fa: [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u89e3\u6790\u5c06\u4e00\u4e2a\u4ee5\u9017\u53f7\u5206\u9694\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u4e00\u4e2a\u5305\u542b\u6574\u6570\u7684\u5217\u8868\u3002\u9996\u5148\uff0c\u8c03\u7528 <code>split(&quot;,&quot;)<\/code> \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u6210\u5b50\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u4f7f\u7528 <code>int(x)<\/code> \u5c06\u6bcf\u4e2a\u5b50\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u6574\u6570\uff0c\u5e76\u5c06\u7ed3\u679c\u5b58\u50a8\u5728\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u9002\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u590d\u6742\u5904\u7406\u7684\u573a\u666f\uff0c\u4f8b\u5982\u9700\u8981\u5bf9\u6bcf\u4e2a\u5b50\u5b57\u7b26\u4e32\u8fdb\u884c\u8f6c\u6362\u3001\u8fc7\u6ee4\u6216\u5176\u4ed6\u64cd\u4f5c\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528 <code>ast.literal_eval<\/code> \u6a21\u5757<\/h2>\n<\/p>\n<p><p><code>ast.literal_eval<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u5b89\u5168\u7684\u65b9\u6cd5\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u6210 Python \u6570\u636e\u7ed3\u6784\u3002\u5b83\u53ef\u4ee5\u89e3\u6790\u5b57\u7b26\u4e32\u5e76\u5c06\u5176\u8f6c\u6362\u6210\u76f8\u5e94\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4f8b\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u5178\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import ast<\/p>\n<p>string = &quot;[1, 2, 3, 4, 5]&quot;<\/p>\n<p>array = ast.literal_eval(string)<\/p>\n<p>print(array)  # \u8f93\u51fa: [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>ast.literal_eval<\/code> \u6a21\u5757\u5c06\u4e00\u4e2a\u8868\u793a\u5217\u8868\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u4e00\u4e2a\u771f\u6b63\u7684\u5217\u8868\u3002<code>literal_eval<\/code> \u65b9\u6cd5\u53ef\u4ee5\u89e3\u6790\u5b57\u7b26\u4e32\u5e76\u5c06\u5176\u8f6c\u6362\u6210\u76f8\u5e94\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4f46\u5b83\u53ea\u9002\u7528\u4e8e\u5b89\u5168\u7684\u5b57\u7b26\u4e32\uff0c\u4e0d\u9002\u7528\u4e8e\u5305\u542b\u4efb\u610f\u4ee3\u7801\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u9002\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p><code>ast.literal_eval<\/code> \u6a21\u5757\u9002\u7528\u4e8e\u9700\u8981\u5c06\u8868\u793a\u6570\u636e\u7ed3\u6784\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u76f8\u5e94\u7684\u6570\u636e\u7c7b\u578b\u7684\u573a\u666f\uff0c\u4f8b\u5982\u4ece\u6587\u4ef6\u6216\u7f51\u7edc\u8bfb\u53d6\u6570\u636e\u65f6\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528 <code>json.loads<\/code> \u65b9\u6cd5<\/h2>\n<\/p>\n<p><p><code>json.loads<\/code> \u65b9\u6cd5\u9002\u7528\u4e8e\u5904\u7406 JSON \u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002\u5b83\u53ef\u4ee5\u5c06 JSON \u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u89e3\u6790\u6210\u76f8\u5e94\u7684 Python \u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982\u5217\u8868\u3001\u5b57\u5178\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>string = &#39;[&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]&#39;<\/p>\n<p>array = json.loads(string)<\/p>\n<p>print(array)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>json.loads<\/code> \u65b9\u6cd5\u5c06\u4e00\u4e2a JSON \u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u4e00\u4e2a\u5305\u542b\u5b57\u7b26\u4e32\u7684\u5217\u8868\u3002<code>json.loads<\/code> \u65b9\u6cd5\u53ef\u4ee5\u89e3\u6790 JSON \u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u5e76\u5c06\u5176\u8f6c\u6362\u6210\u76f8\u5e94\u7684 Python \u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h3>\u9002\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p><code>json.loads<\/code> \u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406 JSON \u683c\u5f0f\u6570\u636e\u7684\u573a\u666f\uff0c\u4f8b\u5982\u4ece API \u83b7\u53d6\u6570\u636e\u65f6\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h2>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u9002\u7528\u4e8e\u5904\u7406\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5339\u914d\u548c\u62c6\u5206\u9700\u6c42\u3002\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u7279\u5b9a\u7684\u6a21\u5f0f\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u6210\u5b50\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u7ed3\u679c\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>string = &quot;apple123banana456cherry&quot;<\/p>\n<p>pattern = r&quot;\\d+&quot;<\/p>\n<p>array = re.split(pattern, string)<\/p>\n<p>print(array)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5c06\u4e00\u4e2a\u5305\u542b\u6570\u5b57\u7684\u5b57\u7b26\u4e32\u62c6\u5206\u6210\u5b50\u5b57\u7b26\u4e32\u3002\u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2a\u5339\u914d\u6570\u5b57\u7684\u6a21\u5f0f <code>r&quot;\\d+&quot;<\/code>\uff0c\u7136\u540e\u4f7f\u7528 <code>re.split<\/code> \u65b9\u6cd5\u6839\u636e\u8be5\u6a21\u5f0f\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u6210\u5b50\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u7ed3\u679c\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u9002\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u9002\u7528\u4e8e\u9700\u8981\u6839\u636e\u7279\u5b9a\u6a21\u5f0f\u62c6\u5206\u5b57\u7b26\u4e32\u7684\u573a\u666f\uff0c\u4f8b\u5982\u4ece\u590d\u6742\u7684\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u7279\u5b9a\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u4f7f\u7528 <code>numpy<\/code> \u6a21\u5757<\/h2>\n<\/p>\n<p><p><code>numpy<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u9ad8\u6548\u7684\u65b9\u5f0f\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u6570\u7ec4\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u5904\u7406\u6570\u503c\u6570\u636e\u7684\u573a\u666f\u3002\u901a\u8fc7 <code>numpy<\/code> \u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u6210 <code>numpy<\/code> \u6570\u7ec4\uff0c\u5e76\u8fdb\u884c\u9ad8\u6548\u7684\u6570\u503c\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>string = &quot;1,2,3,4,5&quot;<\/p>\n<p>array = np.fromstring(string, dtype=int, sep=&quot;,&quot;)<\/p>\n<p>print(array)  # \u8f93\u51fa: [1 2 3 4 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>numpy<\/code> \u6a21\u5757\u5c06\u4e00\u4e2a\u4ee5\u9017\u53f7\u5206\u9694\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u4e00\u4e2a\u5305\u542b\u6574\u6570\u7684 <code>numpy<\/code> \u6570\u7ec4\u3002\u9996\u5148\uff0c\u8c03\u7528 <code>np.fromstring<\/code> \u65b9\u6cd5\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u6210 <code>numpy<\/code> \u6570\u7ec4\uff0c\u5e76\u6307\u5b9a\u6570\u636e\u7c7b\u578b\u4e3a <code>int<\/code> \u548c\u5206\u9694\u7b26\u4e3a\u9017\u53f7\u3002<\/p>\n<\/p>\n<p><h3>\u9002\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p><code>numpy<\/code> \u6a21\u5757\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u6548\u5904\u7406\u6570\u503c\u6570\u636e\u7684\u573a\u666f\uff0c\u4f8b\u5982\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u65f6\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u4f7f\u7528 <code>pandas<\/code> \u6a21\u5757<\/h2>\n<\/p>\n<p><p><code>pandas<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u5f3a\u5927\u7684\u65b9\u5f0f\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u6570\u7ec4\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u5904\u7406\u8868\u683c\u6570\u636e\u7684\u573a\u666f\u3002\u901a\u8fc7 <code>pandas<\/code> \u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u6210 <code>pandas<\/code> \u6570\u636e\u6846\uff0c\u5e76\u8fdb\u884c\u5404\u79cd\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>string = &quot;1,2,3,4,5&quot;<\/p>\n<p>array = pd.Series(string.split(&quot;,&quot;)).astype(int).tolist()<\/p>\n<p>print(array)  # \u8f93\u51fa: [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>pandas<\/code> \u6a21\u5757\u5c06\u4e00\u4e2a\u4ee5\u9017\u53f7\u5206\u9694\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u4e00\u4e2a\u5305\u542b\u6574\u6570\u7684\u5217\u8868\u3002\u9996\u5148\uff0c\u8c03\u7528 <code>split(&quot;,&quot;)<\/code> \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u6210\u5b50\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u4f7f\u7528 <code>pandas<\/code> \u6a21\u5757\u5c06\u5176\u8f6c\u6362\u6210 <code>pandas<\/code> \u7cfb\u5217\uff0c\u5e76\u5c06\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u6210 <code>int<\/code>\uff0c\u6700\u540e\u8c03\u7528 <code>tolist<\/code> \u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u6210\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u9002\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p><code>pandas<\/code> \u6a21\u5757\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u8868\u683c\u6570\u636e\u7684\u573a\u666f\uff0c\u4f8b\u5982\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u548c\u5206\u6790\u65f6\u3002<\/p>\n<\/p>\n<p><h2>\u7ed3\u8bba<\/h2>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u6570\u7ec4\uff0c\u5305\u62ec <code>split()<\/code> \u65b9\u6cd5\u3001\u5217\u8868\u89e3\u6790\u3001<code>ast.literal_eval<\/code> \u6a21\u5757\u3001<code>json.loads<\/code> \u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001<code>numpy<\/code> \u6a21\u5757\u548c <code>pandas<\/code> \u6a21\u5757\u3002\u4e0d\u540c\u7684\u65b9\u6cd5\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5728\u5904\u7406\u5b57\u7b26\u4e32\u8f6c\u6362\u65f6\uff0c\u4e86\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u9ad8\u6548\u5730\u5b8c\u6210\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\u5206\u5272\u6210\u6570\u7ec4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>split()<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u6b64\u65b9\u6cd5\u4f1a\u6839\u636e\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u5206\u6210\u591a\u4e2a\u90e8\u5206\uff0c\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\u3002\u4f8b\u5982\uff0c<code>my_string = &quot;apple,banana,cherry&quot;<\/code>\u53ef\u4ee5\u901a\u8fc7<code>my_string.split(&#39;,&#39;)<\/code>\u8f6c\u6362\u4e3a<code>[&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/code>\u3002<\/p>\n<p><strong>Python\u4e2d\u662f\u5426\u6709\u5176\u4ed6\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>split()<\/code>\u65b9\u6cd5\uff0cPython\u8fd8\u652f\u6301\u4f7f\u7528<code>list()<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u7b26\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u5b57\u7b26\u4e32<code>&quot;hello&quot;<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>list(&quot;hello&quot;)<\/code>\u83b7\u5f97<code>[&#39;h&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;]<\/code>\u3002\u8fd9\u5bf9\u4e8e\u5904\u7406\u5355\u4e2a\u5b57\u7b26\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u7279\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\uff1f<\/strong><br \/>\u5982\u679c\u5b57\u7b26\u4e32\u5305\u542b\u7279\u5b9a\u683c\u5f0f\u7684\u6570\u636e\uff0c\u4f8b\u5982JSON\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u4e2d\u7684<code>loads()<\/code>\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u4f8b\u5982\uff0c<code>import json<\/code>\u540e\uff0c\u4f7f\u7528<code>json.loads(&#39;[&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]&#39;)<\/code>\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aPython\u5217\u8868\u3002\u786e\u4fdd\u5b57\u7b26\u4e32\u7b26\u5408JSON\u683c\u5f0f\uff0c\u4ee5\u4fbf\u6b63\u786e\u89e3\u6790\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u5305\u62ec\u4f7f\u7528 split() \u65b9\u6cd5\u3001\u5217\u8868\u89e3\u6790\u3001\u4ee5\u53ca\u4f7f\u7528 ast [&hellip;]","protected":false},"author":3,"featured_media":1110505,"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\/1110499"}],"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=1110499"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1110499\/revisions"}],"predecessor-version":[{"id":1110506,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1110499\/revisions\/1110506"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1110505"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1110499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1110499"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1110499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}