{"id":1145091,"date":"2025-01-08T23:05:21","date_gmt":"2025-01-08T15:05:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1145091.html"},"modified":"2025-01-08T23:05:25","modified_gmt":"2025-01-08T15:05:25","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%b0%86%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%bd%ac%e6%8d%a2%e6%88%90%e6%95%b0%e5%ad%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1145091.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u6570\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181841\/faa35b7a-fe1c-4709-9d09-19a7cb933457.webp\" alt=\"python\u4e2d\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u6570\u5b57\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u6570\u5b57\u7684\u5e38\u89c1\u65b9\u6cd5\u6709\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>int()<\/code>\u3001<code>float()<\/code>\u3001\u4ee5\u53ca\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u590d\u6742\u7684\u8f6c\u6362<\/strong>\u3002\u5176\u4e2d\uff0c<code>int()<\/code>\u548c<code>float()<\/code>\u51fd\u6570\u662f\u6700\u5e38\u7528\u548c\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u5206\u522b\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u548c\u6d6e\u70b9\u6570\u3002<strong>\u4f7f\u7528int()\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u5b83\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8c03\u7528int()\u51fd\u6570\u5e76\u4f20\u5165\u5b57\u7b26\u4e32\u53c2\u6570<\/strong>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>num_str = &quot;123&quot;<\/p>\n<p>num_int = int(num_str)<\/p>\n<p>print(num_int)  # \u8f93\u51fa: 123<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570<\/h3>\n<\/p>\n<p><p><strong>1. \u76f4\u63a5\u8f6c\u6362<\/strong><\/p>\n<\/p>\n<p><p><code>int()<\/code>\u51fd\u6570\u662f\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u6574\u6570\u7684\u6700\u76f4\u63a5\u65b9\u6cd5\u3002\u4f60\u53ea\u9700\u5c06\u5b57\u7b26\u4e32\u4f20\u9012\u7ed9<code>int()<\/code>\u51fd\u6570\u5373\u53ef\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b57\u7b26\u4e32\u4e2d\u53ea\u80fd\u5305\u542b\u6570\u5b57\u5b57\u7b26\uff0c\u5426\u5219\u4f1a\u5f15\u53d1ValueError\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num_str = &quot;456&quot;<\/p>\n<p>try:<\/p>\n<p>    num_int = int(num_str)<\/p>\n<p>    print(num_int)  # \u8f93\u51fa: 456<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;\u5b57\u7b26\u4e32\u65e0\u6cd5\u8f6c\u6362\u4e3a\u6574\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u5904\u7406\u53ef\u80fd\u7684\u5f02\u5e38<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6709\u65f6\u5019\u5b57\u7b26\u4e32\u53ef\u80fd\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u6b64\u65f6\u76f4\u63a5\u8f6c\u6362\u4f1a\u5f15\u53d1\u5f02\u5e38\u3002\u4e3a\u4e86\u4fdd\u8bc1\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try...except<\/code>\u8bed\u53e5\u6765\u5904\u7406\u53ef\u80fd\u7684\u5f02\u5e38\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 = int(num_str)<\/p>\n<p>    print(num_int)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;\u5b57\u7b26\u4e32\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u65e0\u6cd5\u8f6c\u6362\u4e3a\u6574\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>float()<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570<\/h3>\n<\/p>\n<p><p><strong>1. \u76f4\u63a5\u8f6c\u6362<\/strong><\/p>\n<\/p>\n<p><p>\u4e0e<code>int()<\/code>\u51fd\u6570\u7c7b\u4f3c\uff0c<code>float()<\/code>\u51fd\u6570\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002\u8fd9\u4e2a\u65b9\u6cd5\u540c\u6837\u7b80\u5355\uff0c\u53ea\u9700\u5c06\u5b57\u7b26\u4e32\u4f20\u9012\u7ed9<code>float()<\/code>\u51fd\u6570\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num_str = &quot;123.45&quot;<\/p>\n<p>try:<\/p>\n<p>    num_float = float(num_str)<\/p>\n<p>    print(num_float)  # \u8f93\u51fa: 123.45<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;\u5b57\u7b26\u4e32\u65e0\u6cd5\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u5904\u7406\u53ef\u80fd\u7684\u5f02\u5e38<\/strong><\/p>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u4e3a\u4e86\u4fdd\u8bc1\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>try...except<\/code>\u8bed\u53e5\u6765\u5904\u7406\u53ef\u80fd\u7684\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num_str = &quot;123.45abc&quot;<\/p>\n<p>try:<\/p>\n<p>    num_float = float(num_str)<\/p>\n<p>    print(num_float)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;\u5b57\u7b26\u4e32\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u65e0\u6cd5\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u590d\u6742\u7684\u8f6c\u6362<\/h3>\n<\/p>\n<p><p><strong>1. \u63d0\u53d6\u6570\u5b57<\/strong><\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u4e86\u6570\u5b57\u4ee5\u5916\u7684\u5b57\u7b26\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u90e8\u5206\uff0c\u7136\u540e\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>num_str = &quot;abc123def&quot;<\/p>\n<p>num_list = re.findall(r&#39;\\d+&#39;, num_str)<\/p>\n<p>if num_list:<\/p>\n<p>    num_int = int(num_list[0])<\/p>\n<p>    print(num_int)  # \u8f93\u51fa: 123<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u5b57\u7b26\u4e32\u4e2d\u6ca1\u6709\u627e\u5230\u6570\u5b57&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u5904\u7406\u5e26\u5c0f\u6570\u70b9\u7684\u5b57\u7b26\u4e32<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5e26\u5c0f\u6570\u70b9\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u63d0\u53d6\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>num_str = &quot;abc123.45def&quot;<\/p>\n<p>num_list = re.findall(r&#39;\\d+\\.\\d+&#39;, num_str)<\/p>\n<p>if num_list:<\/p>\n<p>    num_float = float(num_list[0])<\/p>\n<p>    print(num_float)  # \u8f93\u51fa: 123.45<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u5b57\u7b26\u4e32\u4e2d\u6ca1\u6709\u627e\u5230\u6d6e\u70b9\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528<code>ast.literal_eval<\/code>\u8fdb\u884c\u5b89\u5168\u7684\u8f6c\u6362<\/h3>\n<\/p>\n<p><p><strong>1. \u76f4\u63a5\u8f6c\u6362<\/strong><\/p>\n<\/p>\n<p><p><code>ast.literal_eval<\/code>\u662f\u4e00\u4e2a\u66f4\u4e3a\u5b89\u5168\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u6570\u5b57\uff0c\u540c\u65f6\u907f\u514d\u4e86<code>eval<\/code>\u7684\u5b89\u5168\u98ce\u9669\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ast<\/p>\n<p>num_str = &quot;123&quot;<\/p>\n<p>try:<\/p>\n<p>    num = ast.literal_eval(num_str)<\/p>\n<p>    print(num)  # \u8f93\u51fa: 123<\/p>\n<p>except (ValueError, SyntaxError):<\/p>\n<p>    print(&quot;\u5b57\u7b26\u4e32\u65e0\u6cd5\u8f6c\u6362\u4e3a\u6570\u5b57&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u5904\u7406\u5305\u542b\u8868\u8fbe\u5f0f\u7684\u5b57\u7b26\u4e32<\/strong><\/p>\n<\/p>\n<p><p><code>ast.literal_eval<\/code>\u4e5f\u53ef\u4ee5\u5904\u7406\u5305\u542b\u8868\u8fbe\u5f0f\u7684\u5b57\u7b26\u4e32\uff0c\u4f46\u5b83\u53ea\u80fd\u5904\u7406\u5b57\u9762\u91cf\u8868\u8fbe\u5f0f\uff0c\u4e0d\u80fd\u5904\u7406\u590d\u6742\u7684Python\u8868\u8fbe\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ast<\/p>\n<p>num_str = &quot;123 + 456&quot;<\/p>\n<p>try:<\/p>\n<p>    num = ast.literal_eval(num_str)<\/p>\n<p>    print(num)<\/p>\n<p>except (ValueError, SyntaxError):<\/p>\n<p>    print(&quot;\u5b57\u7b26\u4e32\u65e0\u6cd5\u8f6c\u6362\u4e3a\u6570\u5b57&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528<code>numpy<\/code>\u8fdb\u884c\u6279\u91cf\u8f6c\u6362<\/h3>\n<\/p>\n<p><p><strong>1. \u5c06\u5b57\u7b26\u4e32\u6570\u7ec4\u8f6c\u6362\u4e3a\u6570\u5b57\u6570\u7ec4<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5904\u7406\u5927\u91cf\u7684\u5b57\u7b26\u4e32\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy<\/code>\u5e93\u8fdb\u884c\u6279\u91cf\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>str_array = np.array([&quot;123&quot;, &quot;456&quot;, &quot;789&quot;])<\/p>\n<p>num_array = str_array.astype(int)<\/p>\n<p>print(num_array)  # \u8f93\u51fa: [123 456 789]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u5904\u7406\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u6570\u7ec4<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy<\/code>\u7684\u5411\u91cf\u5316\u64cd\u4f5c\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>str_array = np.array([&quot;123&quot;, &quot;456abc&quot;, &quot;789&quot;])<\/p>\n<p>def <a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_int_conversion(s):<\/p>\n<p>    try:<\/p>\n<p>        return int(s)<\/p>\n<p>    except ValueError:<\/p>\n<p>        return np.nan  # \u4f7f\u7528nan\u8868\u793a\u8f6c\u6362\u5931\u8d25<\/p>\n<p>num_array = np.array([safe_int_conversion(s) for s in str_array])<\/p>\n<p>print(num_array)  # \u8f93\u51fa: [123. nan 789.]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528<code>pandas<\/code>\u8fdb\u884c\u6570\u636e\u6846\u7684\u8f6c\u6362<\/h3>\n<\/p>\n<p><p><strong>1. \u5c06\u6570\u636e\u6846\u4e2d\u7684\u5b57\u7b26\u4e32\u5217\u8f6c\u6362\u4e3a\u6570\u5b57\u5217<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6570\u636e\u6846\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {&#39;num_str&#39;: [&quot;123&quot;, &quot;456&quot;, &quot;789&quot;]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>df[&#39;num_int&#39;] = df[&#39;num_str&#39;].astype(int)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u5904\u7406\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\u7684\u6570\u636e\u6846<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\u7684\u6570\u636e\u6846\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u7684<code>apply<\/code>\u65b9\u6cd5\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {&#39;num_str&#39;: [&quot;123&quot;, &quot;456abc&quot;, &quot;789&quot;]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>def safe_int_conversion(s):<\/p>\n<p>    try:<\/p>\n<p>        return int(s)<\/p>\n<p>    except ValueError:<\/p>\n<p>        return np.nan  # \u4f7f\u7528nan\u8868\u793a\u8f6c\u6362\u5931\u8d25<\/p>\n<p>df[&#39;num_int&#39;] = df[&#39;num_str&#39;].apply(safe_int_conversion)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u5728Python\u4e2d\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u6570\u5b57\u7684\u65b9\u6cd5\u975e\u5e38\u591a\u6837\uff0c\u9009\u62e9\u9002\u5408\u81ea\u5df1\u7684\u65b9\u6cd5\u662f\u5173\u952e<\/strong>\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u8f6c\u6362\uff0c\u4f7f\u7528<code>int()<\/code>\u548c<code>float()<\/code>\u51fd\u6570\u662f\u6700\u76f4\u63a5\u7684\u65b9\u5f0f\uff1b\u5bf9\u4e8e\u590d\u6742\u7684\u8f6c\u6362\uff0c\u53ef\u4ee5\u501f\u52a9\u6b63\u5219\u8868\u8fbe\u5f0f\u3001<code>ast.literal_eval<\/code>\u3001<code>numpy<\/code>\u548c<code>pandas<\/code>\u7b49\u5de5\u5177\u3002<strong>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u8003\u8651\u5230\u5b57\u7b26\u4e32\u53ef\u80fd\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u8fdb\u884c\u5f02\u5e38\u5904\u7406\u662f\u975e\u5e38\u91cd\u8981\u7684<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5404\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u6570\u5b57\uff0c\u4ece\u800c\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u4e0b\u7684\u9700\u6c42\u3002\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u4e0d\u4ec5\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387\uff0c\u8fd8\u80fd\u6709\u6548\u5730\u5904\u7406\u5f02\u5e38\u60c5\u51b5\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\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\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\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u6216\u6d6e\u70b9\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32<code>&quot;123&quot;<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>int(&quot;123&quot;)<\/code>\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570123\uff1b\u5982\u679c\u5b57\u7b26\u4e32\u662f<code>&quot;123.45&quot;<\/code>\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528<code>float(&quot;123.45&quot;)<\/code>\u5c06\u5176\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570123.45\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b57\u7b26\u4e32\u4e2d\u7684\u5185\u5bb9\u5fc5\u987b\u7b26\u5408\u6570\u5b57\u683c\u5f0f\uff0c\u5426\u5219\u5c06\u4f1a\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<p><strong>\u5728\u5b57\u7b26\u4e32\u8f6c\u6362\u8fc7\u7a0b\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u975e\u6570\u5b57\u5b57\u7b26\uff1f<\/strong><br \/>\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u4f8b\u5982\u5b57\u6bcd\u6216\u7279\u6b8a\u7b26\u53f7\uff0c\u76f4\u63a5\u4f7f\u7528<code>int()<\/code>\u6216<code>float()<\/code>\u51fd\u6570\u5c06\u4f1a\u5bfc\u81f4<code>ValueError<\/code>\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u5728\u8f6c\u6362\u4e4b\u524d\u4f7f\u7528<code>str.isdigit()<\/code>\u65b9\u6cd5\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u53ea\u5305\u542b\u6570\u5b57\uff0c\u6216\u8005\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\u6765\u6355\u83b7\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try...except<\/code>\u7ed3\u6784\u6765\u5904\u7406\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u7684\u6f5c\u5728\u9519\u8bef\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5c06\u591a\u4e2a\u6570\u5b57\u5b57\u7b26\u4e32\u5408\u5e76\u4e3a\u4e00\u4e2a\u6570\u5b57\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u5c06\u591a\u4e2a\u6570\u5b57\u5b57\u7b26\u4e32\u8fde\u63a5\u8d77\u6765\u5e76\u518d\u8fdb\u884c\u8f6c\u6362\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u5c06\u4e24\u4e2a\u5b57\u7b26\u4e32<code>&quot;123&quot;<\/code>\u548c<code>&quot;456&quot;<\/code>\u5408\u5e76\u4e3a<code>&quot;123456&quot;<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>int(&quot;123456&quot;)<\/code>\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u5229\u7528\u5b57\u7b26\u4e32\u7684\u62fc\u63a5\u529f\u80fd\uff0c\u5982<code>str1 + str2<\/code>\uff0c\u7136\u540e\u8fdb\u884c\u8f6c\u6362\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5c06\u591a\u4e2a\u6570\u5b57\u90e8\u5206\u7ec4\u5408\u6210\u4e00\u4e2a\u5b8c\u6574\u6570\u5b57\u7684\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u6570\u5b57\u7684\u5e38\u89c1\u65b9\u6cd5\u6709\u4f7f\u7528\u5185\u7f6e\u51fd\u6570int()\u3001float()\u3001\u4ee5\u53ca\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c [&hellip;]","protected":false},"author":3,"featured_media":1145106,"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\/1145091"}],"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=1145091"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1145091\/revisions"}],"predecessor-version":[{"id":1145107,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1145091\/revisions\/1145107"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1145106"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1145091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1145091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1145091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}