{"id":1069361,"date":"2024-12-31T16:51:18","date_gmt":"2024-12-31T08:51:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1069361.html"},"modified":"2024-12-31T16:51:21","modified_gmt":"2024-12-31T08:51:21","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%94%b9%e6%88%90%e5%85%83%e7%bb%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1069361.html","title":{"rendered":"Python\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u6539\u6210\u5143\u7ec4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/c2fcf495-02ad-48e1-9ba6-d0ba7ee3b38c.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"Python\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u6539\u6210\u5143\u7ec4\" \/><\/p>\n<p><p> <strong>Python\u5c06\u5b57\u7b26\u4e32\u6539\u6210\u5143\u7ec4\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570eval()\u3001ast.literal_eval()\u3001tuple()\u7b49\u3002\u5176\u4e2d\uff0c<\/strong>\u5efa\u8bae\u4f7f\u7528ast.literal_eval()<strong>\uff0c\u56e0\u4e3a\u5b83\u66f4\u52a0\u5b89\u5168\u548c\u53ef\u9760\u3002<\/strong> \u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u4ee5\u53ca\u5b83\u4eec\u7684\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528eval()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>eval()\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u6267\u884c\u4e00\u4e2a\u5b57\u7b26\u4e32\u8868\u8fbe\u5f0f\uff0c\u5e76\u8fd4\u56de\u8868\u8fbe\u5f0f\u7684\u503c\u3002\u4f7f\u7528eval()\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5143\u7ec4\u975e\u5e38\u7b80\u5355\uff0c\u4f46\u8fd9\u79cd\u65b9\u6cd5\u5b58\u5728\u5b89\u5168\u9690\u60a3\uff0c\u56e0\u4e3aeval()\u4f1a\u6267\u884c\u4f20\u5165\u7684\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u6076\u610f\u4ee3\u7801\uff0c\u53ef\u80fd\u4f1a\u5bf9\u7cfb\u7edf\u9020\u6210\u5371\u5bb3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;(1, 2, 3)&quot;<\/p>\n<p>tuple_result = eval(string)<\/p>\n<p>print(tuple_result)  # \u8f93\u51fa: (1, 2, 3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u7b80\u5355\u6613\u7528\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5b58\u5728\u5b89\u5168\u98ce\u9669\uff0c\u4e0d\u5efa\u8bae\u5728\u5904\u7406\u4e0d\u53ef\u4fe1\u7684\u8f93\u5165\u65f6\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528ast.literal_eval()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>ast.literal_eval()\u662fPython\u6807\u51c6\u5e93ast\u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u53ef\u4ee5\u5b89\u5168\u5730\u89e3\u6790\u7531\u5b57\u7b26\u4e32\u8868\u793a\u7684Python\u57fa\u672c\u6570\u636e\u7ed3\u6784\u3002\u4e0eeval()\u4e0d\u540c\uff0cast.literal_eval()\u53ea\u4f1a\u89e3\u6790\u5b57\u9762\u5e38\u91cf\uff0c\u6bd4\u5982\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5143\u7ec4\u3001\u5217\u8868\u3001\u5b57\u5178\u7b49\uff0c\u800c\u4e0d\u4f1a\u6267\u884c\u5176\u4ed6\u4ee3\u7801\u3002\u56e0\u6b64\uff0c\u4f7f\u7528ast.literal_eval()\u66f4\u52a0\u5b89\u5168\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ast<\/p>\n<p>string = &quot;(1, 2, 3)&quot;<\/p>\n<p>tuple_result = ast.literal_eval(string)<\/p>\n<p>print(tuple_result)  # \u8f93\u51fa: (1, 2, 3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5b89\u5168\u53ef\u9760\uff0c\u9002\u7528\u4e8e\u5904\u7406\u4e0d\u53ef\u4fe1\u7684\u8f93\u5165\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981\u5bfc\u5165ast\u6a21\u5757\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4f7f\u7528tuple()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u5b57\u7b26\u4e32\u8868\u793a\u7684\u662f\u4e00\u4e2a\u9017\u53f7\u5206\u9694\u7684\u5143\u7d20\u5e8f\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528split()\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u5217\u8868\uff0c\u7136\u540e\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5143\u7ec4\u3002\u4e0d\u8fc7\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4ec5\u9002\u7528\u4e8e\u7b80\u5355\u7684\u60c5\u51b5\uff0c\u4e0d\u9002\u5408\u5904\u7406\u5d4c\u5957\u6570\u636e\u7ed3\u6784\u6216\u5305\u542b\u7279\u6b8a\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;1, 2, 3&quot;<\/p>\n<p>tuple_result = tuple(string.split(&quot;, &quot;))<\/p>\n<p>print(tuple_result)  # \u8f93\u51fa: (&#39;1&#39;, &#39;2&#39;, &#39;3&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u7b80\u5355\u6613\u7528\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4ec5\u9002\u7528\u4e8e\u7b80\u5355\u7684\u5b57\u7b26\u4e32\uff0c\u4e0d\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u5d4c\u5957\u6570\u636e\u7ed3\u6784\u6216\u5305\u542b\u7279\u6b8a\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5c06\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u5143\u7ec4\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7075\u6d3b\u5730\u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u7684\u6a21\u5f0f\uff0c\u4f46\u7f16\u5199\u548c\u8c03\u8bd5\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u80fd\u6bd4\u8f83\u590d\u6742\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>string = &quot;(1, 2, 3)&quot;<\/p>\n<p>pattern = r&quot;\\((.*?)\\)&quot;<\/p>\n<p>match = re.search(pattern, string)<\/p>\n<p>if match:<\/p>\n<p>    tuple_result = tuple(map(int, match.group(1).split(&quot;, &quot;)))<\/p>\n<p>    print(tuple_result)  # \u8f93\u51fa: (1, 2, 3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u7075\u6d3b\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u5b9a\u6a21\u5f0f\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u7f16\u5199\u548c\u8c03\u8bd5\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u80fd\u6bd4\u8f83\u590d\u6742\u3002<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u624b\u52a8\u89e3\u6790\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u7279\u6b8a\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u624b\u52a8\u89e3\u6790\u5b57\u7b26\u4e32\uff0c\u5c06\u5176\u8f6c\u6362\u4e3a\u5143\u7ec4\u3002\u624b\u52a8\u89e3\u6790\u9002\u7528\u4e8e\u7279\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff0c\u4f46\u5b9e\u73b0\u8d77\u6765\u53ef\u80fd\u6bd4\u8f83\u7e41\u7410\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;(1, 2, 3)&quot;<\/p>\n<p>string = string.strip(&quot;()&quot;)<\/p>\n<p>elements = string.split(&quot;, &quot;)<\/p>\n<p>tuple_result = tuple(map(int, elements))<\/p>\n<p>print(tuple_result)  # \u8f93\u51fa: (1, 2, 3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u5b9a\u5236\u89e3\u6790\u8fc7\u7a0b\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5b9e\u73b0\u590d\u6742\uff0c\u4e0d\u9002\u5408\u901a\u7528\u60c5\u51b5\u3002<\/li>\n<\/ul>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5c06\u5b57\u7b26\u4e32\u6539\u6210\u5143\u7ec4\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\u3002<strong>\u63a8\u8350\u4f7f\u7528ast.literal_eval()<\/strong>\uff0c\u56e0\u4e3a\u5b83\u65e2\u5b89\u5168\u53c8\u53ef\u9760\uff0c\u9002\u5408\u5904\u7406\u5404\u79cd\u590d\u6742\u60c5\u51b5\u3002eval()\u867d\u7136\u7b80\u5355\u6613\u7528\uff0c\u4f46\u5b58\u5728\u5b89\u5168\u98ce\u9669\uff1btuple()\u51fd\u6570\u9002\u7528\u4e8e\u7b80\u5355\u60c5\u51b5\uff0c\u4f46\u4e0d\u9002\u5408\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784\uff1b\u6b63\u5219\u8868\u8fbe\u5f0f\u548c\u624b\u52a8\u89e3\u6790\u867d\u7136\u7075\u6d3b\uff0c\u4f46\u5b9e\u73b0\u8d77\u6765\u53ef\u80fd\u6bd4\u8f83\u590d\u6742\u3002<\/p>\n<\/p>\n<p><p>\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5e76\u786e\u4fdd\u5728\u5904\u7406\u4e0d\u53ef\u4fe1\u7684\u8f93\u5165\u65f6\u4f7f\u7528\u5b89\u5168\u7684\u65b9\u6cd5\uff0c\u5982ast.literal_eval()\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u5728Python\u4e2d\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5143\u7ec4\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\u8f6c\u6362\u4e3a\u5143\u7ec4\uff1f<\/strong><br \/>\u8981\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5143\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528<code>eval()<\/code>\u51fd\u6570\u6216<code>ast.literal_eval()<\/code>\u65b9\u6cd5\u3002<code>eval()<\/code>\u53ef\u4ee5\u76f4\u63a5\u6267\u884c\u5b57\u7b26\u4e32\u8868\u8fbe\u5f0f\uff0c\u4f46\u8981\u5c0f\u5fc3\u4f7f\u7528\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u6267\u884c\u6076\u610f\u4ee3\u7801\u3002\u66f4\u5b89\u5168\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>ast.literal_eval()<\/code>\uff0c\u5b83\u53ea\u4f1a\u89e3\u6790Python\u5b57\u9762\u91cf\u7ed3\u6784\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import ast\n\n# \u4f7f\u7528 eval()\nstring_tuple = &quot;(1, 2, 3)&quot;\ntuple_result = eval(string_tuple)\n\n# \u4f7f\u7528 ast.literal_eval()\nstring_tuple_<a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a> = &quot;(4, 5, 6)&quot;\ntuple_result_safe = ast.literal_eval(string_tuple_safe)\n\nprint(tuple_result)         # \u8f93\u51fa: (1, 2, 3)\nprint(tuple_result_safe)    # \u8f93\u51fa: (4, 5, 6)\n<\/code><\/pre>\n<p><strong>\u5982\u679c\u5b57\u7b26\u4e32\u683c\u5f0f\u4e0d\u6b63\u786e\uff0c\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u5728\u5c1d\u8bd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5143\u7ec4\u65f6\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u683c\u5f0f\u4e0d\u7b26\u5408\u5143\u7ec4\u7684\u8981\u6c42\uff08\u4f8b\u5982\uff0c\u7f3a\u5c11\u62ec\u53f7\u6216\u5305\u542b\u975e\u6cd5\u5b57\u7b26\uff09\uff0c\u5c06\u5bfc\u81f4\u9519\u8bef\u3002\u4e3a\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u6765\u6355\u6349\u5e76\u5904\u7406\u8fd9\u4e9b\u9519\u8bef\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">string_tuple_invalid = &quot;(1, 2, 3&quot;\n\ntry:\n    result = ast.literal_eval(string_tuple_invalid)\nexcept (ValueError, SyntaxError) as e:\n    print(&quot;\u5b57\u7b26\u4e32\u683c\u5f0f\u9519\u8bef:&quot;, e)\n<\/code><\/pre>\n<p><strong>\u662f\u5426\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u5b57\u7b26\u4e32\u5230\u5143\u7ec4\u7684\u8f6c\u6362\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>eval()<\/code>\u548c<code>ast.literal_eval()<\/code>\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5b57\u7b26\u4e32\u64cd\u4f5c\u6765\u624b\u52a8\u89e3\u6790\u5143\u7ec4\u3002\u53ef\u4ee5\u4f7f\u7528<code>str.split()<\/code>\u65b9\u6cd5\u5206\u5272\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a\u5143\u7ec4\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">string_tuple_manual = &quot;1, 2, 3&quot;\ntuple_result_manual = tuple(map(int, string_tuple_manual.split(&#39;,&#39;)))\n\nprint(tuple_result_manual)  # \u8f93\u51fa: (1, 2, 3)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\uff0c\u4f46\u9700\u8981\u786e\u4fdd\u5b57\u7b26\u4e32\u5185\u5bb9\u662f\u6709\u6548\u7684\u53ef\u8f6c\u6362\u7c7b\u578b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5c06\u5b57\u7b26\u4e32\u6539\u6210\u5143\u7ec4\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570eval()\u3001ast.literal_eval() [&hellip;]","protected":false},"author":3,"featured_media":1069372,"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\/1069361"}],"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=1069361"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1069361\/revisions"}],"predecessor-version":[{"id":1069377,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1069361\/revisions\/1069377"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1069372"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1069361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1069361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1069361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}