{"id":1138546,"date":"2025-01-08T22:01:00","date_gmt":"2025-01-08T14:01:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1138546.html"},"modified":"2025-01-08T22:01:03","modified_gmt":"2025-01-08T14:01:03","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%b0%86%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%bc%ba%e5%88%b6%e8%bd%ac%e4%b8%ba%e6%95%b0%e5%ad%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1138546.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u5f3a\u5236\u8f6c\u4e3a\u6570\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102127\/3dd804df-55df-47ab-8b31-76a09452437d.webp\" alt=\"python\u4e2d\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u5f3a\u5236\u8f6c\u4e3a\u6570\u5b57\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5c06\u5b57\u7b26\u4e32\u5f3a\u5236\u8f6c\u6362\u4e3a\u6570\u5b57\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570int()\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570float()\u3001\u5904\u7406\u5f02\u5e38<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528int()\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u6216\u4f7f\u7528float()\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u5b9e\u7528\u7684\u4ee3\u7801\u793a\u4f8b\u548c\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528int()\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u4f7f\u7528int()\u51fd\u6570\u662f\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u7684\u6700\u76f4\u63a5\u65b9\u6cd5\u3002int()\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u5b57\u7b26\u4e32\u53c2\u6570\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u5982\u679c\u5b57\u7b26\u4e32\u4e0d\u80fd\u88ab\u6210\u529f\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u4f1a\u629b\u51faValueError\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string_num = &quot;123&quot;<\/p>\n<p>integer_num = int(string_num)<\/p>\n<p>print(integer_num)  # \u8f93\u51fa\uff1a123<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528float()\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u5f53\u5b57\u7b26\u4e32\u8868\u793a\u7684\u662f\u6d6e\u70b9\u6570\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528float()\u51fd\u6570\u8fdb\u884c\u8f6c\u6362\u3002float()\u51fd\u6570\u7c7b\u4f3c\u4e8eint()\u51fd\u6570\uff0c\u4f46\u5b83\u4f1a\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string_num = &quot;123.45&quot;<\/p>\n<p>float_num = float(string_num)<\/p>\n<p>print(float_num)  # \u8f93\u51fa\uff1a123.45<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u5904\u7406\u5f02\u5e38<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5b57\u7b26\u4e32\u53ef\u80fd\u65e0\u6cd5\u8f6c\u6362\u4e3a\u6709\u6548\u7684\u6570\u5b57\u3002\u56e0\u6b64\uff0c\u5efa\u8bae\u4f7f\u7528try-except\u5757\u6765\u5904\u7406\u53ef\u80fd\u7684\u5f02\u5e38\u3002\u8fd9\u6837\u53ef\u4ee5\u9632\u6b62\u7a0b\u5e8f\u56e0\u8f6c\u6362\u5931\u8d25\u800c\u5d29\u6e83\uff0c\u5e76\u5141\u8bb8\u5f00\u53d1\u8005\u63d0\u4f9b\u66f4\u53cb\u597d\u7684\u9519\u8bef\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def convert_to_int(string_num):<\/p>\n<p>    try:<\/p>\n<p>        return int(string_num)<\/p>\n<p>    except ValueError:<\/p>\n<p>        print(f&quot;\u4e0d\u80fd\u5c06\u5b57\u7b26\u4e32 &#39;{string_num}&#39; \u8f6c\u6362\u4e3a\u6574\u6570&quot;)<\/p>\n<p>        return None<\/p>\n<p>def convert_to_float(string_num):<\/p>\n<p>    try:<\/p>\n<p>        return float(string_num)<\/p>\n<p>    except ValueError:<\/p>\n<p>        print(f&quot;\u4e0d\u80fd\u5c06\u5b57\u7b26\u4e32 &#39;{string_num}&#39; \u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570&quot;)<\/p>\n<p>        return None<\/p>\n<p>string_num = &quot;123a&quot;<\/p>\n<p>print(convert_to_int(string_num))  # \u8f93\u51fa\uff1a\u4e0d\u80fd\u5c06\u5b57\u7b26\u4e32 &#39;123a&#39; \u8f6c\u6362\u4e3a\u6574\u6570<\/p>\n<p>print(convert_to_float(string_num))  # \u8f93\u51fa\uff1a\u4e0d\u80fd\u5c06\u5b57\u7b26\u4e32 &#39;123a&#39; \u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u5904\u7406\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\u7684\u5b57\u7b26\u4e32<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5b57\u7b26\u4e32\u53ef\u80fd\u5305\u542b\u4e00\u4e9b\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u6bd4\u5982\u8d27\u5e01\u7b26\u53f7\u6216\u9017\u53f7\u3002\u8fd9\u65f6\uff0c\u53ef\u4ee5\u5148\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u9884\u5904\u7406\uff0c\u53bb\u6389\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u518d\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def preprocess_string(string_num):<\/p>\n<p>    # \u79fb\u9664\u6240\u6709\u975e\u6570\u5b57\u5b57\u7b26\uff08\u4fdd\u7559\u5c0f\u6570\u70b9\uff09<\/p>\n<p>    clean_string = re.sub(r&#39;[^\\d.]&#39;, &#39;&#39;, string_num)<\/p>\n<p>    return clean_string<\/p>\n<p>string_num = &quot;$1,234.56&quot;<\/p>\n<p>clean_string_num = preprocess_string(string_num)<\/p>\n<p>float_num = float(clean_string_num)<\/p>\n<p>print(float_num)  # \u8f93\u51fa\uff1a1234.56<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u6b8a\u6570\u5b57\u683c\u5f0f\u8f6c\u6362\u4e3a\u6570\u5b57<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u53ef\u80fd\u4f1a\u4f7f\u7528\u7279\u6b8a\u683c\u5f0f\u8868\u793a\uff0c\u5982\u79d1\u5b66\u8ba1\u6570\u6cd5\u3002Python\u7684float()\u51fd\u6570\u53ef\u4ee5\u81ea\u52a8\u5904\u7406\u8fd9\u4e9b\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string_num = &quot;1.23e4&quot;<\/p>\n<p>float_num = float(string_num)<\/p>\n<p>print(float_num)  # \u8f93\u51fa\uff1a12300.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5c06\u5b57\u7b26\u4e32\u5f3a\u5236\u8f6c\u6362\u4e3a\u6570\u5b57\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u6700\u5e38\u7528\u7684\u5305\u62ec\u4f7f\u7528int()\u51fd\u6570\u3001\u4f7f\u7528float()\u51fd\u6570\u548c\u5904\u7406\u5f02\u5e38\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u8fd8\u53ef\u4ee5\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u9884\u5904\u7406\uff0c\u4ee5\u53bb\u9664\u975e\u6570\u5b57\u5b57\u7b26\u6216\u5904\u7406\u7279\u6b8a\u6570\u5b57\u683c\u5f0f\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u786e\u4fdd\u5b57\u7b26\u4e32\u8f6c\u6362\u8fc7\u7a0b\u7684\u7a33\u5b9a\u6027\u548c\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<p><p><strong>\u5173\u952e\u6b65\u9aa4\u548c\u6ce8\u610f\u4e8b\u9879<\/strong>\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528int()\u51fd\u6570\u8f6c\u6362\u6574\u6570\u5b57\u7b26\u4e32<\/strong>\uff1a\u9002\u7528\u4e8e\u5b57\u7b26\u4e32\u5b8c\u5168\u7531\u6570\u5b57\u7ec4\u6210\u7684\u60c5\u51b5\u3002<\/li>\n<li><strong>\u4f7f\u7528float()\u51fd\u6570\u8f6c\u6362\u6d6e\u70b9\u6570\u5b57\u7b26\u4e32<\/strong>\uff1a\u9002\u7528\u4e8e\u5b57\u7b26\u4e32\u8868\u793a\u7684\u6570\u5b57\u5305\u542b\u5c0f\u6570\u70b9\u6216\u4f7f\u7528\u79d1\u5b66\u8ba1\u6570\u6cd5\u7684\u60c5\u51b5\u3002<\/li>\n<li><strong>\u5904\u7406\u53ef\u80fd\u7684\u5f02\u5e38<\/strong>\uff1a\u4f7f\u7528try-except\u5757\u5904\u7406ValueError\u5f02\u5e38\uff0c\u786e\u4fdd\u7a0b\u5e8f\u4e0d\u4f1a\u56e0\u8f6c\u6362\u5931\u8d25\u800c\u5d29\u6e83\u3002<\/li>\n<li><strong>\u9884\u5904\u7406\u5b57\u7b26\u4e32<\/strong>\uff1a\u5728\u8f6c\u6362\u4e4b\u524d\u79fb\u9664\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u4ee5\u786e\u4fdd\u5b57\u7b26\u4e32\u53ef\u4ee5\u6210\u529f\u8f6c\u6362\u4e3a\u6570\u5b57\u3002<\/li>\n<li><strong>\u5904\u7406\u7279\u6b8a\u683c\u5f0f<\/strong>\uff1a\u7406\u89e3\u5e76\u5904\u7406\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u6b8a\u6570\u5b57\u683c\u5f0f\uff0c\u5982\u79d1\u5b66\u8ba1\u6570\u6cd5\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\uff0c\u5e76\u5e94\u5bf9\u5404\u79cd\u53ef\u80fd\u7684\u5f02\u5e38\u548c\u7279\u6b8a\u60c5\u51b5\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\u6570\u5b57\u3002\u5982\u679c\u5b57\u7b26\u4e32\u8868\u793a\u7684\u662f\u4e00\u4e2a\u6574\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\uff0c\u4f8b\u5982\uff1a<code>number = int(&quot;123&quot;)<\/code>\u3002\u5bf9\u4e8e\u5305\u542b\u5c0f\u6570\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528<code>float()<\/code>\u51fd\u6570\uff0c\u4f8b\u5982\uff1a<code>decimal_number = float(&quot;123.45&quot;)<\/code>\u3002\u6ce8\u610f\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u4e0d\u6b63\u786e\uff0c\u8fd9\u4e9b\u51fd\u6570\u4f1a\u5f15\u53d1<code>ValueError<\/code>\u5f02\u5e38\u3002<\/p>\n<p><strong>\u5728\u5b57\u7b26\u4e32\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u5982\u4f55\u5904\u7406\u5f02\u5e38\uff1f<\/strong><br \/>\u5728\u8f6c\u6362\u5b57\u7b26\u4e32\u4e3a\u6570\u5b57\u65f6\uff0c\u82e5\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u4e0d\u7b26\u5408\u6570\u5b57\u683c\u5f0f\uff0c\u7a0b\u5e8f\u4f1a\u629b\u51fa\u5f02\u5e38\u3002\u4e3a\u4e86\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try...except<\/code>\u8bed\u53e5\u6765\u5904\u7406\u5f02\u5e38\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">try:\n    number = int(&quot;abc&quot;)\nexcept ValueError:\n    print(&quot;\u65e0\u6cd5\u8f6c\u6362\u4e3a\u6570\u5b57\uff0c\u8bf7\u68c0\u67e5\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u80fd\u6709\u6548\u5730\u6355\u83b7\u9519\u8bef\uff0c\u5e76\u7ed9\u51fa\u7528\u6237\u53cb\u597d\u7684\u63d0\u793a\u3002<\/p>\n<p><strong>\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u7a7a\u683c\u6216\u5176\u4ed6\u5b57\u7b26\uff0c\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u7a7a\u683c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>strip()<\/code>\u65b9\u6cd5\u53bb\u9664\u591a\u4f59\u7684\u7a7a\u683c\u3002\u4f8b\u5982\uff1a<code>number = int(&quot;  123  &quot;.strip())<\/code>\u3002\u5bf9\u4e8e\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u5148\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6216\u5176\u4ed6\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5\u6765\u6e05\u7406\u6570\u636e\uff0c\u4ee5\u786e\u4fdd\u8f6c\u6362\u65f6\u4e0d\u4f1a\u5f15\u53d1\u9519\u8bef\u3002\u4f7f\u7528<code>re<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u6709\u6548\u63d0\u53d6\u6570\u5b57\u90e8\u5206\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5c06\u5b57\u7b26\u4e32\u5f3a\u5236\u8f6c\u6362\u4e3a\u6570\u5b57\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570int()\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570float()\u3001\u5904\u7406\u5f02\u5e38 [&hellip;]","protected":false},"author":3,"featured_media":1138553,"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\/1138546"}],"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=1138546"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1138546\/revisions"}],"predecessor-version":[{"id":1138556,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1138546\/revisions\/1138556"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1138553"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1138546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1138546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1138546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}