{"id":1077538,"date":"2025-01-08T12:03:11","date_gmt":"2025-01-08T04:03:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1077538.html"},"modified":"2025-01-08T12:03:13","modified_gmt":"2025-01-08T04:03:13","slug":"%e5%a6%82%e4%bd%95%e6%8a%8a%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%8f%98%e6%88%90%e6%95%b4%e6%95%b0python-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1077538.html","title":{"rendered":"\u5982\u4f55\u628a\u5b57\u7b26\u4e32\u53d8\u6210\u6574\u6570python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181541\/de06d18e-80c4-4d9b-96ca-7cfddc5b9b25.webp\" alt=\"\u5982\u4f55\u628a\u5b57\u7b26\u4e32\u53d8\u6210\u6574\u6570python\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5982\u4f7f\u7528int()\u51fd\u6570\u3001\u5904\u7406\u5f02\u5e38\u60c5\u51b5\u3001\u5229\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5185\u7f6e\u7684int()\u51fd\u6570\uff0c\u5b83\u80fd\u591f\u8f7b\u677e\u5730\u5c06\u8868\u793a\u6570\u5b57\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u5177\u4f53\u5b9e\u73b0\u65b9\u5f0f\u5982\u4e0b\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528int()\u51fd\u6570\u662f\u6700\u5e38\u89c1\u4e14\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u5c06\u5b57\u7b26\u4e32\u201c123\u201d\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u53ea\u9700\u4f7f\u7528int(&quot;123&quot;)\u5373\u53ef\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u8be5\u65b9\u6cd5\u4f1a\u629b\u51faValueError\u5f02\u5e38\u3002\u56e0\u6b64\uff0c\u5efa\u8bae\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u6dfb\u52a0\u5f02\u5e38\u5904\u7406\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u9c81\u68d2\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528int()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u7684\u5185\u7f6e\u51fd\u6570int()\u662f\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u7684\u4e3b\u8981\u65b9\u6cd5\u3002\u5b83\u4e0d\u4ec5\u7b80\u5355\u6613\u7528\uff0c\u800c\u4e14\u6548\u7387\u9ad8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string_number = &quot;123&quot;<\/p>\n<p>integer_number = int(string_number)<\/p>\n<p>print(integer_number)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u4f1a\u8f93\u51fa123\uff0c\u8fd9\u662f\u5c06\u5b57\u7b26\u4e32&quot;123&quot;\u6210\u529f\u8f6c\u6362\u4e3a\u6574\u6570\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>\u5f02\u5e38\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5b57\u7b26\u4e32\u53ef\u80fd\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u6b64\u65f6\u76f4\u63a5\u4f7f\u7528int()\u51fd\u6570\u4f1a\u629b\u51faValueError\u5f02\u5e38\u3002\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528try-except\u8bed\u53e5\u8fdb\u884c\u5f02\u5e38\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string_number = &quot;123abc&quot;<\/p>\n<p>try:<\/p>\n<p>    integer_number = int(string_number)<\/p>\n<p>    print(integer_number)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;\u5b57\u7b26\u4e32\u4e2d\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><p>\u4e0a\u9762\u7684\u4ee3\u7801\u4f1a\u8f93\u51fa\u201c\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u65e0\u6cd5\u8f6c\u6362\u4e3a\u6574\u6570\u201d\uff0c\u56e0\u4e3a\u5b57\u7b26\u4e32&quot;123abc&quot;\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6570\u5b57<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u4ece\u5305\u542b\u6570\u5b57\u548c\u975e\u6570\u5b57\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u51fa\u6570\u5b57\u90e8\u5206\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>string_number = &quot;abc123def&quot;<\/p>\n<p>match = re.search(r&#39;\\d+&#39;, string_number)<\/p>\n<p>if match:<\/p>\n<p>    integer_number = int(match.group())<\/p>\n<p>    print(integer_number)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u5b57\u7b26\u4e32\u4e2d\u4e0d\u5305\u542b\u6570\u5b57&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u4f1a\u8f93\u51fa123\uff0c\u8fd9\u662f\u4ece\u5b57\u7b26\u4e32&quot;abc123def&quot;\u4e2d\u63d0\u53d6\u51fa\u6570\u5b57\u5e76\u8f6c\u6362\u4e3a\u6574\u6570\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u7279\u6b8a\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5230\u6574\u6570\u7684\u8f6c\u6362\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def custom_str_to_int(string_number):<\/p>\n<p>    # \u79fb\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u683c<\/p>\n<p>    string_number = string_number.strip()<\/p>\n<p>    # \u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u7a7a<\/p>\n<p>    if not string_number:<\/p>\n<p>        r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u4e3a\u7a7a&quot;)<\/p>\n<p>    # \u5224\u65ad\u662f\u5426\u4e3a\u8d1f\u6570<\/p>\n<p>    is_negative = False<\/p>\n<p>    if string_number[0] == &#39;-&#39;:<\/p>\n<p>        is_negative = True<\/p>\n<p>        string_number = string_number[1:]<\/p>\n<p>    elif string_number[0] == &#39;+&#39;:<\/p>\n<p>        string_number = string_number[1:]<\/p>\n<p>    # \u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u68c0\u67e5\u662f\u5426\u4e3a\u6570\u5b57<\/p>\n<p>    for char in string_number:<\/p>\n<p>        if not char.isdigit():<\/p>\n<p>            raise ValueError(&quot;\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26&quot;)<\/p>\n<p>    # \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570<\/p>\n<p>    integer_number = 0<\/p>\n<p>    for char in string_number:<\/p>\n<p>        integer_number = integer_number * 10 + (ord(char) - ord(&#39;0&#39;))<\/p>\n<p>    # \u6839\u636e\u8d1f\u6570\u6807\u5fd7\u8c03\u6574\u7ed3\u679c<\/p>\n<p>    if is_negative:<\/p>\n<p>        integer_number = -integer_number<\/p>\n<p>    return integer_number<\/p>\n<h2><strong>\u6d4b\u8bd5\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong><\/h2>\n<p>try:<\/p>\n<p>    string_number = &quot;  -123 &quot;<\/p>\n<p>    integer_number = custom_str_to_int(string_number)<\/p>\n<p>    print(integer_number)<\/p>\n<p>except ValueError as e:<\/p>\n<p>    print(e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u4f1a\u8f93\u51fa-123\uff0c\u8fd9\u662f\u81ea\u5b9a\u4e49\u51fd\u6570\u6210\u529f\u5c06\u5b57\u7b26\u4e32&quot;  -123 &quot;\u8f6c\u6362\u4e3a\u6574\u6570\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7ed3\u5408\u5176\u4ed6\u6570\u636e\u7c7b\u578b<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6709\u65f6\u5019\u5b57\u7b26\u4e32\u4e2d\u53ef\u80fd\u5305\u542b\u5c0f\u6570\u70b9\u6216\u8005\u5176\u4ed6\u975e\u6574\u6570\u6570\u5b57\u3002\u6211\u4eec\u53ef\u4ee5\u5148\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\uff0c\u518d\u8f6c\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string_number = &quot;123.45&quot;<\/p>\n<p>try:<\/p>\n<p>    float_number = float(string_number)<\/p>\n<p>    integer_number = int(float_number)<\/p>\n<p>    print(integer_number)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;\u5b57\u7b26\u4e32\u4e2d\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><p>\u4e0a\u9762\u7684\u4ee3\u7801\u4f1a\u8f93\u51fa123\uff0c\u8fd9\u662f\u5c06\u5b57\u7b26\u4e32&quot;123.45&quot;\u5148\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\uff0c\u518d\u8f6c\u6362\u4e3a\u6574\u6570\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u7279\u6b8a\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u7279\u6b8a\u60c5\u51b5\u4e0b\uff0c\u5b57\u7b26\u4e32\u53ef\u80fd\u8868\u793a\u7684\u4e0d\u662f\u5341\u8fdb\u5236\u6570\u5b57\uff0c\u4f8b\u5982\u4e8c\u8fdb\u5236\u3001\u516b\u8fdb\u5236\u6216\u5341\u516d\u8fdb\u5236\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528int()\u51fd\u6570\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6765\u6307\u5b9a\u8fdb\u5236\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">binary_string = &quot;1101&quot;<\/p>\n<p>octal_string = &quot;17&quot;<\/p>\n<p>hex_string = &quot;1A&quot;<\/p>\n<p>binary_number = int(binary_string, 2)<\/p>\n<p>octal_number = int(octal_string, 8)<\/p>\n<p>hex_number = int(hex_string, 16)<\/p>\n<p>print(binary_number)  # \u8f93\u51fa13<\/p>\n<p>print(octal_number)   # \u8f93\u51fa15<\/p>\n<p>print(hex_number)     # \u8f93\u51fa26<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u5c06\u8868\u793a\u4e0d\u540c\u8fdb\u5236\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\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\u9879\u5e38\u89c1\u7684\u4efb\u52a1\u3002\u4f7f\u7528int()\u51fd\u6570\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u4f46\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6dfb\u52a0\u5f02\u5e38\u5904\u7406\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u4ee5\u53ca\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u90fd\u662f\u63d0\u9ad8\u4ee3\u7801\u5065\u58ee\u6027\u548c\u7075\u6d3b\u6027\u7684\u6709\u6548\u624b\u6bb5\u3002\u7406\u89e3\u5e76\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5c06\u6709\u52a9\u4e8e\u66f4\u597d\u5730\u5904\u7406\u5b57\u7b26\u4e32\u548c\u6574\u6570\u4e4b\u95f4\u7684\u8f6c\u6362\u9700\u6c42\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\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\u53ea\u9700\u5c06\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9<code>int()<\/code>\uff0c\u5b83\u4f1a\u8fd4\u56de\u5bf9\u5e94\u7684\u6574\u6570\u3002\u4f8b\u5982\uff0c<code>int(&quot;123&quot;)<\/code>\u5c06\u8fd4\u56de\u6574\u6570<code>123<\/code>\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b57\u7b26\u4e32\u4e2d\u5e94\u53ea\u5305\u542b\u6570\u5b57\u5b57\u7b26\u4ee5\u53ca\u53ef\u9009\u7684\u8d1f\u53f7\uff0c\u5426\u5219\u4f1a\u5f15\u53d1<code>ValueError<\/code>\u5f02\u5e38\u3002<\/p>\n<p><strong>\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u4f1a\u5931\u8d25\uff1f<\/strong><br \/>\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u53ef\u80fd\u4f1a\u5931\u8d25\u7684\u60c5\u51b5\u5305\u62ec\uff1a\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u5982\u5b57\u6bcd\u6216\u7279\u6b8a\u7b26\u53f7\uff0c\u6216\u8005\u5b57\u7b26\u4e32\u4e3a\u7a7a\u3002\u8fd9\u4e9b\u90fd\u4f1a\u5bfc\u81f4<code>ValueError<\/code>\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u5f02\u5e38\uff0c\u5e76\u786e\u4fdd\u8f93\u5165\u5b57\u7b26\u4e32\u662f\u6709\u6548\u7684\u6570\u5b57\u683c\u5f0f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5305\u542b\u7a7a\u683c\u6216\u5176\u4ed6\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u7a7a\u683c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>strip()<\/code>\u65b9\u6cd5\u53bb\u6389\u524d\u540e\u7684\u7a7a\u683c\u540e\u518d\u8fdb\u884c\u8f6c\u6362\u3002\u4f8b\u5982\uff0c<code>int(&quot;  456  &quot;.strip())<\/code>\u4f1a\u6210\u529f\u8f6c\u6362\u4e3a\u6574\u6570<code>456<\/code>\u3002\u5bf9\u4e8e\u5305\u542b\u5176\u4ed6\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fc7\u6ee4\u51fa\u6570\u5b57\u90e8\u5206\uff0c\u6216\u8005\u5148\u8fdb\u884c\u5b57\u7b26\u4e32\u9884\u5904\u7406\uff0c\u786e\u4fdd\u53ea\u4fdd\u7559\u53ef\u8f6c\u6362\u7684\u90e8\u5206\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5982\u4f7f\u7528int()\u51fd\u6570\u3001\u5904\u7406\u5f02\u5e38\u60c5\u51b5\u3001\u5229\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u3002\u6700\u5e38\u7528\u7684\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":1077546,"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\/1077538"}],"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=1077538"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1077538\/revisions"}],"predecessor-version":[{"id":1077548,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1077538\/revisions\/1077548"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1077546"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1077538"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1077538"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1077538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}