{"id":1134716,"date":"2025-01-08T21:18:16","date_gmt":"2025-01-08T13:18:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1134716.html"},"modified":"2025-01-08T21:18:19","modified_gmt":"2025-01-08T13:18:19","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%b0%86%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%8f%98%e4%b8%ba%e6%95%b0%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1134716.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u53d8\u4e3a\u6570\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103539\/21329aa3-da3c-4cb5-8f72-26bfcc522631.webp\" alt=\"python\u4e2d\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u53d8\u4e3a\u6570\u503c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u503c\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528int()\u3001float()\u3001\u4ee5\u53ca\u5176\u4ed6\u7c7b\u578b\u8f6c\u6362\u51fd\u6570\u3002<\/strong> \u8fd9\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u8c03\u7528\u8fd9\u4e9b\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u5c06\u5b57\u7b26\u4e32\u201c123\u201d\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528int(&quot;123&quot;)\u3002\u82e5\u5b57\u7b26\u4e32\u5305\u542b\u5c0f\u6570\u70b9\uff0c\u53ef\u4ee5\u4f7f\u7528float(&quot;123.45&quot;)\u5c06\u5176\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u5e93\u548c\u65b9\u6cd5\u6765\u5904\u7406\u66f4\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5230\u6570\u503c\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><p><strong>int()\u51fd\u6570\u7684\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p>int()\u51fd\u6570\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u5b83\u53ef\u4ee5\u5c06\u4e00\u4e2a\u8868\u793a\u6574\u6570\u7684\u5b57\u7b26\u4e32\u76f4\u63a5\u8f6c\u6362\u4e3a\u6574\u6570\u7c7b\u578b\u3002\u5982\u679c\u5b57\u7b26\u4e32\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\u6216\u5c0f\u6570\u70b9\uff0c\u5219\u4f1a\u629b\u51faValueError\u5f02\u5e38\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num_str = &quot;123&quot;<\/p>\n<p>num = int(num_str)<\/p>\n<p>print(num)  # \u8f93\u51fa\uff1a123<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>float()\u51fd\u6570\u7684\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p>float()\u51fd\u6570\u4e0eint()\u7c7b\u4f3c\uff0c\u4f46\u5b83\u7528\u4e8e\u5c06\u8868\u793a\u6d6e\u70b9\u6570\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u7c7b\u578b\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">float_str = &quot;123.45&quot;<\/p>\n<p>num = float(float_str)<\/p>\n<p>print(num)  # \u8f93\u51fa\uff1a123.45<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e00\u3001\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u5728Python\u4e2d\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u6765\u81ea\u7528\u6237\u8f93\u5165\u6216\u6587\u4ef6\u7684\u6570\u636e\u65f6\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528int()\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>1.1\u3001\u4f7f\u7528int()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>int()\u51fd\u6570\u53ef\u4ee5\u5c06\u4e00\u4e2a\u8868\u793a\u6574\u6570\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u6216\u5b57\u7b26\u4e32\u8868\u793a\u7684\u6570\u503c\u8d85\u51fa\u4e86\u6574\u6570\u8303\u56f4\uff0c\u5219\u4f1a\u629b\u51faValueError\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num_str = &quot;456&quot;<\/p>\n<p>num = int(num_str)<\/p>\n<p>print(num)  # \u8f93\u51fa\uff1a456<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u5b57\u7b26\u4e32\u8868\u793a\u7684\u662f\u5341\u516d\u8fdb\u5236\u3001\u516b\u8fdb\u5236\u6216\u4e8c\u8fdb\u5236\u6570\uff0c\u53ef\u4ee5\u5728int()\u51fd\u6570\u4e2d\u6307\u5b9a\u76f8\u5e94\u7684\u57fa\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">hex_str = &quot;1A&quot;<\/p>\n<p>num = int(hex_str, 16)<\/p>\n<p>print(num)  # \u8f93\u51fa\uff1a26<\/p>\n<p>oct_str = &quot;27&quot;<\/p>\n<p>num = int(oct_str, 8)<\/p>\n<p>print(num)  # \u8f93\u51fa\uff1a23<\/p>\n<p>bin_str = &quot;1101&quot;<\/p>\n<p>num = int(bin_str, 2)<\/p>\n<p>print(num)  # \u8f93\u51fa\uff1a13<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>1.2\u3001\u5904\u7406\u5f02\u5e38\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u65f6\uff0c\u9700\u8981\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u60c5\u51b5\u3002\u4f8b\u5982\uff0c\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u53ef\u80fd\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u6b64\u65f6\u9700\u8981\u6355\u83b7ValueError\u5f02\u5e38\u5e76\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num_str = &quot;456abc&quot;<\/p>\n<p>try:<\/p>\n<p>    num = int(num_str)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(f&quot;\u65e0\u6cd5\u5c06\u5b57\u7b26\u4e32 &#39;{num_str}&#39; \u8f6c\u6362\u4e3a\u6574\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>1.3\u3001\u6279\u91cf\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u4e32\u90fd\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_list = [&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;]<\/p>\n<p>num_list = [int(num_str) for num_str in str_list]<\/p>\n<p>print(num_list)  # \u8f93\u51fa\uff1a[1, 2, 3, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u5728Python\u4e2d\u540c\u6837\u975e\u5e38\u5e38\u89c1\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5305\u542b\u5c0f\u6570\u7684\u6570\u503c\u65f6\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528float()\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>2.1\u3001\u4f7f\u7528float()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>float()\u51fd\u6570\u53ef\u4ee5\u5c06\u4e00\u4e2a\u8868\u793a\u6d6e\u70b9\u6570\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\uff08\u9664\u4e86\u5c0f\u6570\u70b9\u548c\u79d1\u5b66\u8bb0\u6570\u6cd5\u4e2d\u7684&#39;e&#39;\u6216&#39;E&#39;\uff09\uff0c\u5219\u4f1a\u629b\u51faValueError\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">float_str = &quot;123.45&quot;<\/p>\n<p>num = float(float_str)<\/p>\n<p>print(num)  # \u8f93\u51fa\uff1a123.45<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2.2\u3001\u5904\u7406\u79d1\u5b66\u8bb0\u6570\u6cd5<\/h3>\n<\/p>\n<p><p>float()\u51fd\u6570\u53ef\u4ee5\u5904\u7406\u79d1\u5b66\u8bb0\u6570\u6cd5\u5f62\u5f0f\u7684\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sci_str = &quot;1.23e4&quot;<\/p>\n<p>num = float(sci_str)<\/p>\n<p>print(num)  # \u8f93\u51fa\uff1a12300.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2.3\u3001\u5904\u7406\u5f02\u5e38\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u65f6\uff0c\u540c\u6837\u9700\u8981\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u60c5\u51b5\u3002\u4f8b\u5982\uff0c\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u53ef\u80fd\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u6b64\u65f6\u9700\u8981\u6355\u83b7ValueError\u5f02\u5e38\u5e76\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">float_str = &quot;123.45abc&quot;<\/p>\n<p>try:<\/p>\n<p>    num = float(float_str)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(f&quot;\u65e0\u6cd5\u5c06\u5b57\u7b26\u4e32 &#39;{float_str}&#39; \u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2.4\u3001\u6279\u91cf\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u4e32\u90fd\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_list = [&quot;1.1&quot;, &quot;2.2&quot;, &quot;3.3&quot;, &quot;4.4&quot;]<\/p>\n<p>num_list = [float(num_str) for num_str in str_list]<\/p>\n<p>print(num_list)  # \u8f93\u51fa\uff1a[1.1, 2.2, 3.3, 4.4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e09\u3001\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u590d\u6742\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u590d\u6742\u6570\uff08complex number\uff09\u5728\u67d0\u4e9b\u79d1\u5b66\u8ba1\u7b97\u548c\u5de5\u7a0b\u5e94\u7528\u4e2d\u975e\u5e38\u91cd\u8981\u3002Python\u63d0\u4f9b\u4e86\u5185\u7f6e\u7684complex()\u51fd\u6570\u6765\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u590d\u6742\u6570\u3002<\/p>\n<\/p>\n<p><h3>3.1\u3001\u4f7f\u7528complex()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>complex()\u51fd\u6570\u53ef\u4ee5\u5c06\u4e00\u4e2a\u8868\u793a\u590d\u6570\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u590d\u6570\u3002\u5982\u679c\u5b57\u7b26\u4e32\u683c\u5f0f\u4e0d\u6b63\u786e\uff0c\u5219\u4f1a\u629b\u51faValueError\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">complex_str = &quot;1+2j&quot;<\/p>\n<p>num = complex(complex_str)<\/p>\n<p>print(num)  # \u8f93\u51fa\uff1a(1+2j)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3.2\u3001\u5904\u7406\u5f02\u5e38\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u590d\u6742\u6570\u65f6\uff0c\u4e5f\u9700\u8981\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u60c5\u51b5\u3002\u4f8b\u5982\uff0c\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u4e0d\u6b63\u786e\uff0c\u6b64\u65f6\u9700\u8981\u6355\u83b7ValueError\u5f02\u5e38\u5e76\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">complex_str = &quot;1+2k&quot;<\/p>\n<p>try:<\/p>\n<p>    num = complex(complex_str)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(f&quot;\u65e0\u6cd5\u5c06\u5b57\u7b26\u4e32 &#39;{complex_str}&#39; \u8f6c\u6362\u4e3a\u590d\u6742\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3.3\u3001\u6279\u91cf\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u4e32\u90fd\u8f6c\u6362\u4e3a\u590d\u6742\u6570\u3002\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_list = [&quot;1+1j&quot;, &quot;2+2j&quot;, &quot;3+3j&quot;, &quot;4+4j&quot;]<\/p>\n<p>num_list = [complex(num_str) for num_str in str_list]<\/p>\n<p>print(num_list)  # \u8f93\u51fa\uff1a[(1+1j), (2+2j), (3+3j), (4+4j)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u56db\u3001\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u914d\u7f6e\u6587\u4ef6\u6216\u7528\u6237\u8f93\u5165\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u5c06&quot;true&quot;\u3001&quot;false&quot;\u7b49\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\u3002<\/p>\n<\/p>\n<p><h3>4.1\u3001\u5b9a\u4e49\u81ea\u5b9a\u4e49\u8f6c\u6362\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u6ca1\u6709\u5185\u7f6e\u51fd\u6570\u76f4\u63a5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u81ea\u5b9a\u4e49\u8f6c\u6362\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def str_to_bool(s):<\/p>\n<p>    if s.lower() in [&#39;true&#39;, &#39;1&#39;, &#39;t&#39;, &#39;y&#39;, &#39;yes&#39;]:<\/p>\n<p>        return True<\/p>\n<p>    elif s.lower() in [&#39;false&#39;, &#39;0&#39;, &#39;f&#39;, &#39;n&#39;, &#39;no&#39;]:<\/p>\n<p>        return False<\/p>\n<p>    else:<\/p>\n<p>        r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(f&quot;\u65e0\u6cd5\u5c06\u5b57\u7b26\u4e32 &#39;{s}&#39; \u8f6c\u6362\u4e3a\u5e03\u5c14\u503c&quot;)<\/p>\n<p>print(str_to_bool(&quot;True&quot;))  # \u8f93\u51fa\uff1aTrue<\/p>\n<p>print(str_to_bool(&quot;no&quot;))    # \u8f93\u51fa\uff1aFalse<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4.2\u3001\u5904\u7406\u5f02\u5e38\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\u65f6\uff0c\u9700\u8981\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u60c5\u51b5\u3002\u4f8b\u5982\uff0c\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u4e0d\u5728\u9884\u5b9a\u4e49\u7684\u5e03\u5c14\u503c\u8303\u56f4\u5185\uff0c\u6b64\u65f6\u9700\u8981\u6355\u83b7ValueError\u5f02\u5e38\u5e76\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">bool_str = &quot;maybe&quot;<\/p>\n<p>try:<\/p>\n<p>    bool_val = str_to_bool(bool_str)<\/p>\n<p>except ValueError as e:<\/p>\n<p>    print(e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4.3\u3001\u6279\u91cf\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u4e32\u90fd\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\u3002\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_list = [&quot;True&quot;, &quot;False&quot;, &quot;yes&quot;, &quot;no&quot;]<\/p>\n<p>bool_list = [str_to_bool(bool_str) for bool_str in str_list]<\/p>\n<p>print(bool_list)  # \u8f93\u51fa\uff1a[True, False, True, False]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e94\u3001\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u65f6\u95f4<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u5e94\u7528\u4e2d\uff0c\u9700\u8981\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u65f6\u95f4\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u65e5\u5fd7\u6587\u4ef6\u6216\u7528\u6237\u8f93\u5165\u7684\u65f6\u95f4\u6570\u636e\u65f6\u3002Python\u63d0\u4f9b\u4e86datetime\u6a21\u5757\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>5.1\u3001\u4f7f\u7528datetime.strptime()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>datetime.strptime()\u51fd\u6570\u53ef\u4ee5\u5c06\u4e00\u4e2a\u8868\u793a\u65e5\u671f\u65f6\u95f4\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3adatetime\u5bf9\u8c61\u3002\u9700\u8981\u63d0\u4f9b\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u8bf4\u660e\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>date_str = &quot;2023-10-01&quot;<\/p>\n<p>date_obj = datetime.strptime(date_str, &quot;%Y-%m-%d&quot;)<\/p>\n<p>print(date_obj)  # \u8f93\u51fa\uff1a2023-10-01 00:00:00<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5.2\u3001\u5904\u7406\u5f02\u5e38\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u65f6\u95f4\u65f6\uff0c\u9700\u8981\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u60c5\u51b5\u3002\u4f8b\u5982\uff0c\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u4e0d\u6b63\u786e\uff0c\u6b64\u65f6\u9700\u8981\u6355\u83b7ValueError\u5f02\u5e38\u5e76\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">date_str = &quot;2023\/10\/01&quot;<\/p>\n<p>try:<\/p>\n<p>    date_obj = datetime.strptime(date_str, &quot;%Y-%m-%d&quot;)<\/p>\n<p>except ValueError as e:<\/p>\n<p>    print(f&quot;\u65e0\u6cd5\u5c06\u5b57\u7b26\u4e32 &#39;{date_str}&#39; \u8f6c\u6362\u4e3a\u65e5\u671f\u65f6\u95f4\uff1a{e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5.3\u3001\u6279\u91cf\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u4e32\u90fd\u8f6c\u6362\u4e3a\u65e5\u671f\u65f6\u95f4\u3002\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">date_list = [&quot;2023-10-01&quot;, &quot;2023-10-02&quot;, &quot;2023-10-03&quot;]<\/p>\n<p>date_objs = [datetime.strptime(date_str, &quot;%Y-%m-%d&quot;) for date_str in date_list]<\/p>\n<p>print(date_objs)  # \u8f93\u51fa\uff1a[datetime.datetime(2023, 10, 1, 0, 0), datetime.datetime(2023, 10, 2, 0, 0), datetime.datetime(2023, 10, 3, 0, 0)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u516d\u3001\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u679a\u4e3e<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u679a\u4e3e\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u914d\u7f6e\u6587\u4ef6\u6216\u7528\u6237\u8f93\u5165\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u5c06\u7279\u5b9a\u5b57\u7b26\u4e32\u6620\u5c04\u5230\u679a\u4e3e\u6210\u5458\u3002<\/p>\n<\/p>\n<p><h3>6.1\u3001\u5b9a\u4e49\u679a\u4e3e\u7c7b<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u679a\u4e3e\u7c7b\u3002\u53ef\u4ee5\u4f7f\u7528enum\u6a21\u5757\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from enum import Enum<\/p>\n<p>class Color(Enum):<\/p>\n<p>    RED = 1<\/p>\n<p>    GREEN = 2<\/p>\n<p>    BLUE = 3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>6.2\u3001\u4f7f\u7528\u679a\u4e3e\u7c7b\u7684\u6784\u9020\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u679a\u4e3e\u7c7b\u7684\u6784\u9020\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u679a\u4e3e\u6210\u5458\u3002\u5982\u679c\u5b57\u7b26\u4e32\u4e0d\u5728\u679a\u4e3e\u6210\u5458\u7684\u540d\u79f0\u5217\u8868\u4e2d\uff0c\u5219\u4f1a\u629b\u51faValueError\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">color_str = &quot;RED&quot;<\/p>\n<p>color = Color[color_str]<\/p>\n<p>print(color)  # \u8f93\u51fa\uff1aColor.RED<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>6.3\u3001\u5904\u7406\u5f02\u5e38\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u679a\u4e3e\u6210\u5458\u65f6\uff0c\u9700\u8981\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u60c5\u51b5\u3002\u4f8b\u5982\uff0c\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u4e0d\u5728\u679a\u4e3e\u6210\u5458\u7684\u540d\u79f0\u5217\u8868\u4e2d\uff0c\u6b64\u65f6\u9700\u8981\u6355\u83b7KeyError\u5f02\u5e38\u5e76\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">color_str = &quot;YELLOW&quot;<\/p>\n<p>try:<\/p>\n<p>    color = Color[color_str]<\/p>\n<p>except KeyError:<\/p>\n<p>    print(f&quot;\u65e0\u6cd5\u5c06\u5b57\u7b26\u4e32 &#39;{color_str}&#39; \u8f6c\u6362\u4e3a\u679a\u4e3e\u6210\u5458&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>6.4\u3001\u6279\u91cf\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u4e32\u90fd\u8f6c\u6362\u4e3a\u679a\u4e3e\u6210\u5458\u3002\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">color_list = [&quot;RED&quot;, &quot;GREEN&quot;, &quot;BLUE&quot;]<\/p>\n<p>colors = [Color[color_str] for color_str in color_list]<\/p>\n<p>print(colors)  # \u8f93\u51fa\uff1a[&lt;Color.RED: 1&gt;, &lt;Color.GREEN: 2&gt;, &lt;Color.BLUE: 3&gt;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e03\u3001\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\u6216\u5143\u7ec4<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u5c06\u4e00\u4e2a\u8868\u793a\u5217\u8868\u6216\u5143\u7ec4\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b9e\u9645\u7684\u5217\u8868\u6216\u5143\u7ec4\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u914d\u7f6e\u6587\u4ef6\u6216\u7528\u6237\u8f93\u5165\u7684\u96c6\u5408\u6570\u636e\u65f6\u3002\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684eval()\u51fd\u6570\u6216ast.literal_eval()\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><h3>7.1\u3001\u4f7f\u7528eval()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>eval()\u51fd\u6570\u53ef\u4ee5\u5c06\u4e00\u4e2a\u8868\u793a\u5217\u8868\u6216\u5143\u7ec4\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b9e\u9645\u7684\u5217\u8868\u6216\u5143\u7ec4\u3002\u7136\u800c\uff0c\u7531\u4e8eeval()\u51fd\u6570\u4f1a\u6267\u884c\u5b57\u7b26\u4e32\u4e2d\u7684\u4ee3\u7801\uff0c\u56e0\u6b64\u5b58\u5728\u5b89\u5168\u98ce\u9669\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list_str = &quot;[1, 2, 3, 4]&quot;<\/p>\n<p>lst = eval(list_str)<\/p>\n<p>print(lst)  # \u8f93\u51fa\uff1a[1, 2, 3, 4]<\/p>\n<p>tuple_str = &quot;(1, 2, 3, 4)&quot;<\/p>\n<p>tup = eval(tuple_str)<\/p>\n<p>print(tup)  # \u8f93\u51fa\uff1a(1, 2, 3, 4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>7.2\u3001\u4f7f\u7528ast.literal_eval()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u907f\u514deval()\u51fd\u6570\u7684\u5b89\u5168\u98ce\u9669\uff0c\u53ef\u4ee5\u4f7f\u7528ast.literal_eval()\u51fd\u6570\u3002\u5b83\u53ea\u4f1a\u89e3\u6790\u5b57\u9762\u91cf\uff0c\u4e0d\u4f1a\u6267\u884c\u5b57\u7b26\u4e32\u4e2d\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ast<\/p>\n<p>list_str = &quot;[1, 2, 3, 4]&quot;<\/p>\n<p>lst = ast.literal_eval(list_str)<\/p>\n<p>print(lst)  # \u8f93\u51fa\uff1a[1, 2, 3, 4]<\/p>\n<p>tuple_str = &quot;(1, 2, 3, 4)&quot;<\/p>\n<p>tup = ast.literal_eval(tuple_str)<\/p>\n<p>print(tup)  # \u8f93\u51fa\uff1a(1, 2, 3, 4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>7.3\u3001\u5904\u7406\u5f02\u5e38\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\u6216\u5143\u7ec4\u65f6\uff0c\u9700\u8981\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u60c5\u51b5\u3002\u4f8b\u5982\uff0c\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u4e0d\u6b63\u786e\uff0c\u6b64\u65f6\u9700\u8981\u6355\u83b7ValueError\u5f02\u5e38\u5e76\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list_str = &quot;[1, 2, 3, 4&quot;<\/p>\n<p>try:<\/p>\n<p>    lst = ast.literal_eval(list_str)<\/p>\n<p>except ValueError as e:<\/p>\n<p>    print(f&quot;\u65e0\u6cd5\u5c06\u5b57\u7b26\u4e32 &#39;{list_str}&#39; \u8f6c\u6362\u4e3a\u5217\u8868\uff1a{e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>7.4\u3001\u6279\u91cf\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u4e32\u90fd\u8f6c\u6362\u4e3a\u5b9e\u9645\u7684\u5217\u8868\u6216\u5143\u7ec4\u3002\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list_strs = [&quot;[1, 2, 3, 4]&quot;, &quot;[5, 6, 7, 8]&quot;]<\/p>\n<p>lists = [ast.literal_eval(list_str) for list_str in list_strs]<\/p>\n<p>print(lists)  # \u8f93\u51fa\uff1a[[1, 2, 3, 4], [5, 6, 7, 8]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u516b\u3001\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u5178<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u5c06\u4e00\u4e2a\u8868\u793a\u5b57\u5178\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b9e\u9645\u7684\u5b57\u5178\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u914d\u7f6e\u6587\u4ef6\u6216\u7528\u6237\u8f93\u5165\u7684\u952e\u503c\u5bf9\u6570\u636e\u65f6\u3002\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684eval()\u51fd\u6570\u6216ast.literal_eval()\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><h3>8.1\u3001\u4f7f\u7528eval()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>eval()\u51fd\u6570\u53ef\u4ee5\u5c06\u4e00\u4e2a\u8868\u793a\u5b57\u5178\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b9e\u9645\u7684\u5b57\u5178\u3002\u7136\u800c\uff0c\u7531\u4e8eeval()\u51fd\u6570\u4f1a\u6267\u884c\u5b57\u7b26\u4e32\u4e2d\u7684\u4ee3\u7801\uff0c\u56e0\u6b64\u5b58\u5728\u5b89\u5168\u98ce\u9669\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dict_str = &quot;{&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}&quot;<\/p>\n<p>d = eval(dict_str)<\/p>\n<p>print(d)  # \u8f93\u51fa\uff1a{&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>8.2\u3001\u4f7f\u7528ast.literal_eval()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u907f\u514deval()\u51fd\u6570\u7684\u5b89\u5168\u98ce\u9669\uff0c\u53ef\u4ee5\u4f7f\u7528ast.literal_eval()\u51fd\u6570\u3002\u5b83\u53ea\u4f1a\u89e3\u6790\u5b57\u9762\u91cf\uff0c\u4e0d\u4f1a\u6267\u884c\u5b57\u7b26\u4e32\u4e2d\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ast<\/p>\n<p>dict_str = &quot;{&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}&quot;<\/p>\n<p>d = ast.literal_eval(dict_str)<\/p>\n<p>print(d)  # \u8f93\u51fa\uff1a{&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>8.3\u3001\u5904\u7406\u5f02\u5e38\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u5178\u65f6\uff0c\u9700\u8981\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u60c5\u51b5\u3002\u4f8b\u5982\uff0c\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u4e0d\u6b63\u786e\uff0c\u6b64\u65f6\u9700\u8981\u6355\u83b7ValueError\u5f02\u5e38\u5e76\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dict_str = &quot;{&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3&quot;<\/p>\n<p>try:<\/p>\n<p>    d = ast.literal_eval(dict_str)<\/p>\n<p>except ValueError as e:<\/p>\n<p>    print(f&quot;\u65e0\u6cd5\u5c06\u5b57\u7b26\u4e32 &#39;{dict_str}&#39; \u8f6c\u6362\u4e3a\u5b57\u5178\uff1a{e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>8.4\u3001\u6279\u91cf\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u4e32\u90fd\u8f6c\u6362\u4e3a\u5b9e\u9645\u7684\u5b57\u5178\u3002\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dict_strs = [&quot;{&#39;a&#39;: 1, &#39;b&#39;: 2}&quot;, &quot;{&#39;c&#39;: 3, &#39;d&#39;: 4}&quot;]<\/p>\n<p>dicts = [ast.literal_eval(dict_str) for dict_str in dict_strs]<\/p>\n<p>print(dicts)  # \u8f93\u51fa\uff1a[{&#39;a&#39;: 1, &#39;b&#39;: 2}, {&#39;c&#39;: 3, &#39;d&#39;: 4}]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e5d\u3001\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5176\u4ed6\u6570\u636e\u7c7b\u578b<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5176\u4ed6\u6570\u636e\u7c7b\u578b\uff0c\u4f8b\u5982\u96c6\u5408\uff08set\uff09\u6216\u81ea\u5b9a\u4e49\u5bf9\u8c61\u3002\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u6216\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><h3>9.1\u3001\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u96c6\u5408<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684set()\u51fd\u6570\u5c06\u4e00\u4e2a\u8868\u793a\u96c6\u5408\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b9e\u9645\u7684\u96c6\u5408\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">set_str = &quot;{1, 2, 3, 4}&quot;<\/p>\n<p>s = eval(set_str)  # \u6ce8\u610f\uff1aeval()\u51fd\u6570\u6709\u5b89\u5168\u98ce\u9669<\/p>\n<p>print(s)  # \u8f93\u51fa\uff1a{1, 2, 3, 4}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e3a\u4e86\u907f\u514deval()\u51fd\u6570\u7684\u5b89\u5168\u98ce\u9669\uff0c\u53ef\u4ee5\u4f7f\u7528ast.literal_eval()\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ast<\/p>\n<p>set_str = &quot;{1, 2, 3, 4}&quot;<\/p>\n<p>s = ast.literal_eval<\/p>\n<p><\/code><\/pre>\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\u6574\u6570\u6216\u6d6e\u70b9\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>int()<\/code>\u548c<code>float()<\/code>\u51fd\u6570\u6765\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u6216\u6d6e\u70b9\u6570\u3002\u4f8b\u5982\uff0c<code>int(&quot;123&quot;)<\/code>\u5c06\u8fd4\u56de\u6574\u6570123\uff0c\u800c<code>float(&quot;123.45&quot;)<\/code>\u5c06\u8fd4\u56de\u6d6e\u70b9\u6570123.45\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b57\u7b26\u4e32\u5fc5\u987b\u662f\u6709\u6548\u7684\u6570\u5b57\u683c\u5f0f\uff0c\u5426\u5219\u4f1a\u5f15\u53d1<code>ValueError<\/code>\u3002<\/p>\n<p><strong>\u5728\u5b57\u7b26\u4e32\u4e2d\u5b58\u5728\u975e\u6570\u5b57\u5b57\u7b26\u65f6\u8be5\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u76f4\u63a5\u8f6c\u6362\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u7ed3\u6784\u6765\u6355\u6349\u5f02\u5e38\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u5904\u7406\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5728\u8f6c\u6362\u524d\u5148\u7528<code>str.replace()<\/code>\u65b9\u6cd5\u53bb\u9664\u4e0d\u9700\u8981\u7684\u5b57\u7b26\uff0c\u6216\u8005\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6570\u5b57\u90e8\u5206\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u5305\u542b\u6570\u5b57\u548c\u5176\u4ed6\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u63d0\u53d6\u4e3a\u6570\u503c\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5757<code>re<\/code>\u6765\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>re.findall(r&#39;\\d+\\.?\\d*&#39;, your_string)<\/code>\u53ef\u4ee5\u627e\u5230\u6240\u6709\u6570\u5b57\u53ca\u5176\u5c0f\u6570\u90e8\u5206\u3002\u63d0\u53d6\u540e\uff0c\u53ef\u4ee5\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a\u6574\u6570\u6216\u6d6e\u70b9\u6570\u3002\u8fd9\u6837\u53ef\u4ee5\u5904\u7406\u66f4\u590d\u6742\u7684\u5b57\u7b26\u4e32\uff0c\u786e\u4fdd\u63d0\u53d6\u5230\u6240\u9700\u7684\u6570\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u503c\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528int()\u3001float()\u3001\u4ee5\u53ca\u5176\u4ed6\u7c7b\u578b\u8f6c\u6362\u51fd\u6570\u3002 \u8fd9\u53ef\u4ee5\u901a [&hellip;]","protected":false},"author":3,"featured_media":1134725,"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\/1134716"}],"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=1134716"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1134716\/revisions"}],"predecessor-version":[{"id":1134726,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1134716\/revisions\/1134726"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1134725"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1134716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1134716"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1134716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}