{"id":1040104,"date":"2024-12-31T12:38:23","date_gmt":"2024-12-31T04:38:23","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1040104.html"},"modified":"2024-12-31T12:38:26","modified_gmt":"2024-12-31T04:38:26","slug":"python%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%a6%82%e4%bd%95%e8%bd%ac%e5%8c%96%e4%b8%ba%e6%95%b4%e5%bd%a2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1040104.html","title":{"rendered":"Python\u5b57\u7b26\u4e32\u5982\u4f55\u8f6c\u5316\u4e3a\u6574\u5f62"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/1eeafb3b-f533-4878-818d-fb3d42a8b9a3.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"Python\u5b57\u7b26\u4e32\u5982\u4f55\u8f6c\u5316\u4e3a\u6574\u5f62\" \/><\/p>\n<p><p> <strong>Python\u5b57\u7b26\u4e32\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u51fd\u6570\u3001\u5f02\u5e38\u5904\u7406\u3001\u5b57\u7b26\u4e32\u5904\u7406\u3001\u591a\u6b21\u5c1d\u8bd5\u8f6c\u6362\u7b49\u65b9\u6cd5\u8f6c\u5316\u4e3a\u6574\u5f62<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u89c1\u548c\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570 <code>int()<\/code>\uff0c\u5b83\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u5316\u4e3a\u6574\u6570\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u51e0\u79cd\u5e38\u7528\u7684\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u573a\u666f\uff0c\u5e76\u7ed9\u51fa\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570 <code>int()<\/code><\/h3>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u7684\u5185\u7f6e\u51fd\u6570 <code>int()<\/code>\uff0c\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u5982\u679c\u5b57\u7b26\u4e32\u5305\u542b\u7684\u5185\u5bb9\u662f\u4e00\u4e2a\u6709\u6548\u7684\u6574\u6570\u8868\u793a\uff0c\u8fd9\u4e2a\u51fd\u6570\u5c06\u8fd4\u56de\u76f8\u5e94\u7684\u6574\u6570\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_num = &quot;123&quot;<\/p>\n<p>int_num = int(str_num)<\/p>\n<p>print(int_num)  # \u8f93\u51fa: 123<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\u76f4\u63a5\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u6216\u8005\u662f\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528 <code>int()<\/code> \u51fd\u6570\u4f1a\u629b\u51fa <code>ValueError<\/code> \u5f02\u5e38\u3002\u56e0\u6b64\uff0c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u901a\u5e38\u4f1a\u7ed3\u5408\u5f02\u5e38\u5904\u7406\u6765\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5f02\u5e38\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fdd\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\uff0c\u5728\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u65f6\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528 <code>try...except<\/code> \u8bed\u53e5\u6765\u6355\u83b7\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_num = &quot;123a&quot;<\/p>\n<p>try:<\/p>\n<p>    int_num = int(str_num)<\/p>\n<p>    print(int_num)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;\u8f6c\u6362\u5931\u8d25\uff1a\u5b57\u7b26\u4e32\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u5b57\u7b26\u4e32 <code>str_num<\/code> \u4e2d\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u7a0b\u5e8f\u5c06\u4e0d\u4f1a\u5d29\u6e83\uff0c\u800c\u662f\u4f1a\u6355\u83b7 <code>ValueError<\/code> \u5f02\u5e38\u5e76\u8f93\u51fa\u63d0\u793a\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u5b57\u7b26\u4e32\u8fc7\u6ee4<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5b57\u7b26\u4e32\u53ef\u80fd\u5305\u542b\u4e00\u4e9b\u975e\u6570\u5b57\u5b57\u7b26\uff08\u5982\u7a7a\u683c\u3001\u9017\u53f7\u7b49\uff09\uff0c\u8fd9\u65f6\u5019\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fc7\u6ee4\u6389\u8fd9\u4e9b\u5b57\u7b26\uff0c\u518d\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>str_num = &quot;12,34&quot;<\/p>\n<p>filtered_str = re.sub(r&#39;\\D&#39;, &#39;&#39;, str_num)<\/p>\n<p>int_num = int(filtered_str)<\/p>\n<p>print(int_num)  # \u8f93\u51fa: 1234<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>re.sub(r&#39;\\D&#39;, &#39;&#39;, str_num)<\/code> \u4f1a\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u975e\u6570\u5b57\u5b57\u7b26\u79fb\u9664\uff0c\u751f\u6210\u4e00\u4e2a\u53ea\u5305\u542b\u6570\u5b57\u5b57\u7b26\u7684\u65b0\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u518d\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u5e26\u6709\u524d\u7f00\u7684\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u6709\u4e9b\u5b57\u7b26\u4e32\u53ef\u80fd\u5305\u542b\u4e00\u4e9b\u524d\u7f00\uff08\u5982\u8d27\u5e01\u7b26\u53f7\u3001\u5355\u4f4d\u7b49\uff09\uff0c\u5728\u8f6c\u6362\u524d\u9700\u8981\u5148\u53bb\u9664\u8fd9\u4e9b\u524d\u7f00\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str_num = &quot;$123&quot;<\/p>\n<p>if str_num.startswith(&quot;$&quot;):<\/p>\n<p>    str_num = str_num[1:]<\/p>\n<p>int_num = int(str_num)<\/p>\n<p>print(int_num)  # \u8f93\u51fa: 123<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u53bb\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u524d\u7f00\uff0c\u786e\u4fdd\u5b57\u7b26\u4e32\u53ea\u5305\u542b\u6570\u5b57\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u591a\u6b21\u5c1d\u8bd5\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u590d\u6742\u7684\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u591a\u6b21\u5c1d\u8bd5\u8f6c\u6362\u4e0d\u540c\u7684\u5b57\u7b26\u4e32\u5f62\u5f0f\u3002\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u6839\u636e\u4e0d\u540c\u7684\u8f6c\u6362\u89c4\u5219\u8fdb\u884c\u5c1d\u8bd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def str_to_int(str_num):<\/p>\n<p>    try:<\/p>\n<p>        return int(str_num)<\/p>\n<p>    except ValueError:<\/p>\n<p>        pass<\/p>\n<p>    try:<\/p>\n<p>        # \u5c1d\u8bd5\u53bb\u9664\u9017\u53f7\u540e\u8f6c\u6362<\/p>\n<p>        filtered_str = str_num.replace(&quot;,&quot;, &quot;&quot;)<\/p>\n<p>        return int(filtered_str)<\/p>\n<p>    except ValueError:<\/p>\n<p>        pass<\/p>\n<p>    try:<\/p>\n<p>        # \u5c1d\u8bd5\u53bb\u9664\u8d27\u5e01\u7b26\u53f7\u540e\u8f6c\u6362<\/p>\n<p>        if str_num.startswith(&quot;$&quot;):<\/p>\n<p>            return int(str_num[1:])<\/p>\n<p>    except ValueError:<\/p>\n<p>        pass<\/p>\n<p>    r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;\u65e0\u6cd5\u8f6c\u6362\u5b57\u7b26\u4e32\u4e3a\u6574\u6570&quot;)<\/p>\n<p>print(str_to_int(&quot;123&quot;))      # \u8f93\u51fa: 123<\/p>\n<p>print(str_to_int(&quot;12,345&quot;))   # \u8f93\u51fa: 12345<\/p>\n<p>print(str_to_int(&quot;$12345&quot;))   # \u8f93\u51fa: 12345<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u51fd\u6570\u5c1d\u8bd5\u4e86\u591a\u79cd\u8f6c\u6362\u89c4\u5219\uff0c\u5982\u679c\u6240\u6709\u89c4\u5219\u90fd\u5931\u8d25\uff0c\u5219\u629b\u51fa <code>ValueError<\/code> \u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u5728Python\u4e2d\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c<strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570 <code>int()<\/code> \u662f\u6700\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u6cd5<\/strong>\u3002\u4f46\u662f\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u7ed3\u5408\u5f02\u5e38\u5904\u7406\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u8fc7\u6ee4\u3001\u53bb\u9664\u524d\u7f00\u7b49\u65b9\u6cd5\uff0c\u786e\u4fdd\u8f6c\u6362\u8fc7\u7a0b\u7684\u9c81\u68d2\u6027\u548c\u7075\u6d3b\u6027\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u6280\u672f\uff0c\u53ef\u4ee5\u6709\u6548\u5904\u7406\u5404\u79cd\u590d\u6742\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u4e2d\u7684\u5b57\u7b26\u4e32\u5b89\u5168\u5730\u8f6c\u6362\u4e3a\u6574\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>int()<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u4e3a\u4e86\u786e\u4fdd\u8f6c\u6362\u7684\u5b89\u5168\u6027\uff0c\u5e94\u8be5\u5148\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u53ea\u5305\u542b\u6570\u5b57\u5b57\u7b26\uff0c\u907f\u514d\u56e0\u65e0\u6548\u8f93\u5165\u5f15\u53d1\u9519\u8bef\u3002\u53ef\u4ee5\u4f7f\u7528<code>str.isdigit()<\/code>\u65b9\u6cd5\u8fdb\u884c\u5224\u65ad\uff0c\u6216\u8005\u5728\u8f6c\u6362\u65f6\u4f7f\u7528<code>try-except<\/code>\u5757\u6765\u6355\u83b7\u53ef\u80fd\u7684\u5f02\u5e38\u3002<\/p>\n<p><strong>\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u7a7a\u683c\u6216\u5176\u4ed6\u5b57\u7b26\uff0c\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u5728\u8f6c\u6362\u4e4b\u524d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>str.strip()<\/code>\u65b9\u6cd5\u53bb\u6389\u5b57\u7b26\u4e32\u4e24\u7aef\u7684\u7a7a\u683c\u3002\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u8f6c\u6362\u5c06\u5931\u8d25\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6216\u5176\u4ed6\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5\u6765\u6e05\u7406\u65e0\u6548\u5b57\u7b26\uff0c\u786e\u4fdd\u6700\u7ec8\u7684\u5b57\u7b26\u4e32\u53ea\u5305\u542b\u53ef\u8f6c\u6362\u7684\u6570\u5b57\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7<code>float()<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\uff0c\u7136\u540e\u518d\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u5c0f\u6570\u7684\u60c5\u51b5\uff0c\u4f46\u8981\u6ce8\u610f\uff0c\u4f7f\u7528<code>float()<\/code>\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7cbe\u5ea6\u635f\u5931\uff0c\u56e0\u6b64\u5728\u9700\u8981\u7cbe\u786e\u6574\u6570\u65f6\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u63a8\u8350\u4f7f\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5b57\u7b26\u4e32\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u51fd\u6570\u3001\u5f02\u5e38\u5904\u7406\u3001\u5b57\u7b26\u4e32\u5904\u7406\u3001\u591a\u6b21\u5c1d\u8bd5\u8f6c\u6362\u7b49\u65b9\u6cd5\u8f6c\u5316\u4e3a\u6574\u5f62\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u89c1\u548c\u76f4\u63a5\u7684\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":1040109,"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\/1040104"}],"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=1040104"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1040104\/revisions"}],"predecessor-version":[{"id":1040112,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1040104\/revisions\/1040112"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1040109"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1040104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1040104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1040104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}