{"id":1181822,"date":"2025-01-15T18:56:15","date_gmt":"2025-01-15T10:56:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1181822.html"},"modified":"2025-01-15T18:56:17","modified_gmt":"2025-01-15T10:56:17","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%af%86%e5%88%ab%e6%95%b0%e5%ad%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1181822.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8bc6\u522b\u6570\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25130032\/8ae72ef1-874d-4f35-ad5b-da9c03050560.webp\" alt=\"python\u4e2d\u5982\u4f55\u8bc6\u522b\u6570\u5b57\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u8bc6\u522b\u6570\u5b57\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u7c7b\u578b\u8f6c\u6362\u3001\u7b2c\u4e09\u65b9\u5e93\u7b49\u3002<\/strong>\u5176\u4e2d\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>isdigit()<\/code>\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u7c7b\u578b\u8f6c\u6362\uff08\u5982\u4f7f\u7528<code>int()<\/code>\u6216<code>float()<\/code>\u51fd\u6570\uff09\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5e93\uff08\u5982NumPy\uff09\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u2014\u2014\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u6570\u5b57\u8bc6\u522b\u3002<\/p>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u5339\u914d\u590d\u6742\u7684\u5b57\u7b26\u4e32\u6a21\u5f0f\u3002Python\u4e2d\u7684<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5bf9\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u652f\u6301\u3002\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7cbe\u786e\u5730\u5339\u914d\u5404\u79cd\u683c\u5f0f\u7684\u6570\u5b57\uff0c\u5305\u62ec\u6574\u6570\u3001\u5c0f\u6570\u3001\u8d1f\u6570\u7b49\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\u6765\u8bc6\u522b\u6570\u5b57\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5bfc\u5165<code>re<\/code>\u6a21\u5757\u3002<\/li>\n<li>\u7f16\u5199\u5339\u914d\u6570\u5b57\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u3002<\/li>\n<li>\u4f7f\u7528<code>re.match()<\/code>\u6216<code>re.search()<\/code>\u51fd\u6570\u5339\u914d\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ol>\n<p><p>\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u6bb5\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8bc6\u522b\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u6709\u6548\u7684\u6570\u5b57\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def is_number(string):<\/p>\n<p>    pattern = r&#39;^-?\\d+(\\.\\d+)?$&#39;<\/p>\n<p>    return re.match(pattern, string) is not None<\/p>\n<p>print(is_number(&quot;123&quot;))       # True<\/p>\n<p>print(is_number(&quot;-123.45&quot;))   # True<\/p>\n<p>print(is_number(&quot;abc123&quot;))    # False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython\u4e2d\u8bc6\u522b\u6570\u5b57\u7684\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<\/h2>\n<\/p>\n<p><h3>1. <code>isdigit()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>isdigit()<\/code>\u662fPython\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u7528\u4e8e\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u53ea\u5305\u542b\u6570\u5b57\u5b57\u7b26\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b83\u53ea\u80fd\u8bc6\u522b\u6b63\u6574\u6570\uff0c\u4e0d\u5305\u62ec\u8d1f\u6570\u3001\u5c0f\u6570\u548c\u5176\u4ed6\u6570\u5b57\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num_str = &quot;12345&quot;<\/p>\n<p>if num_str.isdigit():<\/p>\n<p>    print(f&quot;{num_str} \u662f\u4e00\u4e2a\u6570\u5b57&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{num_str} \u4e0d\u662f\u4e00\u4e2a\u6570\u5b57&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. <code>isnumeric()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>isnumeric()<\/code>\u65b9\u6cd5\u4e0e<code>isdigit()<\/code>\u7c7b\u4f3c\uff0c\u4f46\u5b83\u53ef\u4ee5\u8bc6\u522b\u66f4\u591a\u7684\u6570\u5b57\u5f62\u5f0f\uff0c\u5982\u7f57\u9a6c\u6570\u5b57\u3001\u5206\u6570\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num_str = &quot;12345&quot;<\/p>\n<p>if num_str.isnumeric():<\/p>\n<p>    print(f&quot;{num_str} \u662f\u4e00\u4e2a\u6570\u5b57&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{num_str} \u4e0d\u662f\u4e00\u4e2a\u6570\u5b57&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. <code>isdecimal()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>isdecimal()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u53ea\u5305\u542b\u5341\u8fdb\u5236\u5b57\u7b26\u3002\u4e0e<code>isdigit()<\/code>\u548c<code>isnumeric()<\/code>\u76f8\u6bd4\uff0c\u5b83\u7684\u9002\u7528\u8303\u56f4\u66f4\u7a84\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num_str = &quot;12345&quot;<\/p>\n<p>if num_str.isdecimal():<\/p>\n<p>    print(f&quot;{num_str} \u662f\u4e00\u4e2a\u6570\u5b57&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{num_str} \u4e0d\u662f\u4e00\u4e2a\u6570\u5b57&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h2>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u5339\u914d\u590d\u6742\u7684\u5b57\u7b26\u4e32\u6a21\u5f0f\u3002Python\u4e2d\u7684<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5bf9\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u652f\u6301\u3002\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7cbe\u786e\u5730\u5339\u914d\u5404\u79cd\u683c\u5f0f\u7684\u6570\u5b57\uff0c\u5305\u62ec\u6574\u6570\u3001\u5c0f\u6570\u3001\u8d1f\u6570\u7b49\u3002<\/p>\n<\/p>\n<p><h3>1. \u5339\u914d\u6574\u6570<\/h3>\n<\/p>\n<p><p>\u8981\u5339\u914d\u6574\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&#39;^-?\\d+$&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u5339\u914d\u5c0f\u6570<\/h3>\n<\/p>\n<p><p>\u8981\u5339\u914d\u5c0f\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&#39;^-?\\d+(\\.\\d+)?$&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u5339\u914d\u79d1\u5b66\u8ba1\u6570\u6cd5<\/h3>\n<\/p>\n<p><p>\u8981\u5339\u914d\u79d1\u5b66\u8ba1\u6570\u6cd5\u8868\u793a\u7684\u6570\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&#39;^-?\\d+(\\.\\d+)?([eE][-+]?\\d+)?$&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u4ee3\u7801\u6bb5\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8bc6\u522b\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u6709\u6548\u7684\u6570\u5b57\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def is_number(string):<\/p>\n<p>    pattern = r&#39;^-?\\d+(\\.\\d+)?([eE][-+]?\\d+)?$&#39;<\/p>\n<p>    return re.match(pattern, string) is not None<\/p>\n<p>print(is_number(&quot;123&quot;))         # True<\/p>\n<p>print(is_number(&quot;-123.45&quot;))     # True<\/p>\n<p>print(is_number(&quot;1.23e10&quot;))     # True<\/p>\n<p>print(is_number(&quot;abc123&quot;))      # False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528\u7c7b\u578b\u8f6c\u6362<\/h2>\n<\/p>\n<p><h3>1. \u4f7f\u7528 <code>int()<\/code> \u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 <code>int()<\/code> \u51fd\u6570\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u5982\u679c\u5b57\u7b26\u4e32\u4e0d\u80fd\u8f6c\u6362\u4e3a\u6709\u6548\u7684\u6574\u6570\uff0c\u5219\u4f1a\u5f15\u53d1 <code>ValueError<\/code> \u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num_str = &quot;12345&quot;<\/p>\n<p>try:<\/p>\n<p>    num = int(num_str)<\/p>\n<p>    print(f&quot;{num_str} \u662f\u4e00\u4e2a\u6574\u6570&quot;)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(f&quot;{num_str} \u4e0d\u662f\u4e00\u4e2a\u6574\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4f7f\u7528 <code>float()<\/code> \u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 <code>float()<\/code> \u51fd\u6570\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002\u5982\u679c\u5b57\u7b26\u4e32\u4e0d\u80fd\u8f6c\u6362\u4e3a\u6709\u6548\u7684\u6d6e\u70b9\u6570\uff0c\u5219\u4f1a\u5f15\u53d1 <code>ValueError<\/code> \u5f02\u5e38\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(num_str)<\/p>\n<p>    print(f&quot;{num_str} \u662f\u4e00\u4e2a\u6d6e\u70b9\u6570&quot;)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(f&quot;{num_str} \u4e0d\u662f\u4e00\u4e2a\u6d6e\u70b9\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u7efc\u5408\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u7efc\u5408\u4f7f\u7528 <code>int()<\/code> \u548c <code>float()<\/code> \u51fd\u6570\u6765\u5224\u65ad\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u6709\u6548\u7684\u6570\u5b57\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 = int(num_str)<\/p>\n<p>    print(f&quot;{num_str} \u662f\u4e00\u4e2a\u6574\u6570&quot;)<\/p>\n<p>except ValueError:<\/p>\n<p>    try:<\/p>\n<p>        num = float(num_str)<\/p>\n<p>        print(f&quot;{num_str} \u662f\u4e00\u4e2a\u6d6e\u70b9\u6570&quot;)<\/p>\n<p>    except ValueError:<\/p>\n<p>        print(f&quot;{num_str} \u4e0d\u662f\u4e00\u4e2a\u6570\u5b57&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h2>\n<\/p>\n<p><h3>1. NumPy\u5e93<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u7684\u4e00\u4e2a\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u503c\u8ba1\u7b97\u529f\u80fd\u3002\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u5224\u65ad\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u6709\u6548\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>def is_number(string):<\/p>\n<p>    try:<\/p>\n<p>        num = np.float64(string)<\/p>\n<p>        return True<\/p>\n<p>    except ValueError:<\/p>\n<p>        return False<\/p>\n<p>print(is_number(&quot;123&quot;))       # True<\/p>\n<p>print(is_number(&quot;-123.45&quot;))   # True<\/p>\n<p>print(is_number(&quot;abc123&quot;))    # False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. pandas\u5e93<\/h3>\n<\/p>\n<p><p>Pandas\u662fPython\u7684\u4e00\u4e2a\u6570\u636e\u5206\u6790\u5e93\uff0c\u4e5f\u63d0\u4f9b\u4e86\u6570\u503c\u8bc6\u522b\u7684\u529f\u80fd\u3002\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u6765\u5224\u65ad\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u6709\u6548\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>def is_number(string):<\/p>\n<p>    return pd.to_numeric(string, errors=&#39;coerce&#39;).notna()<\/p>\n<p>print(is_number(&quot;123&quot;))       # True<\/p>\n<p>print(is_number(&quot;-123.45&quot;))   # True<\/p>\n<p>print(is_number(&quot;abc123&quot;))    # False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u4f7f\u7528 <code>decimal<\/code> \u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>decimal<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u7528\u4e8e\u5341\u8fdb\u5236\u6d6e\u70b9\u8fd0\u7b97\u7684 <code>Decimal<\/code> \u6570\u636e\u7c7b\u578b\u3002\u53ef\u4ee5\u4f7f\u7528 <code>decimal.Decimal()<\/code> \u65b9\u6cd5\u6765\u5224\u65ad\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u6709\u6548\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from decimal import Decimal, InvalidOperation<\/p>\n<p>def is_number(string):<\/p>\n<p>    try:<\/p>\n<p>        num = Decimal(string)<\/p>\n<p>        return True<\/p>\n<p>    except InvalidOperation:<\/p>\n<p>        return False<\/p>\n<p>print(is_number(&quot;123&quot;))       # True<\/p>\n<p>print(is_number(&quot;-123.45&quot;))   # True<\/p>\n<p>print(is_number(&quot;abc123&quot;))    # False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u66f4\u591a\u6b63\u5219\u8868\u8fbe\u5f0f\u793a\u4f8b<\/h2>\n<\/p>\n<p><h3>1. \u5339\u914d\u6574\u6570\u548c\u5c0f\u6570\uff08\u5305\u62ec\u6b63\u8d1f\u6570\uff09<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&#39;^-?\\d+(\\.\\d+)?$&#39;<\/p>\n<p>def is_number(string):<\/p>\n<p>    return re.match(pattern, string) is not None<\/p>\n<p>print(is_number(&quot;123&quot;))       # True<\/p>\n<p>print(is_number(&quot;-123.45&quot;))   # True<\/p>\n<p>print(is_number(&quot;abc123&quot;))    # False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u5339\u914d\u79d1\u5b66\u8ba1\u6570\u6cd5\u8868\u793a\u7684\u6570\u5b57<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&#39;^-?\\d+(\\.\\d+)?([eE][-+]?\\d+)?$&#39;<\/p>\n<p>def is_number(string):<\/p>\n<p>    return re.match(pattern, string) is not None<\/p>\n<p>print(is_number(&quot;1.23e10&quot;))   # True<\/p>\n<p>print(is_number(&quot;-1.23E-10&quot;)) # True<\/p>\n<p>print(is_number(&quot;123abc&quot;))    # False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u5339\u914d\u5e26\u5343\u4f4d\u5206\u9694\u7b26\u7684\u6570\u5b57<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&#39;^-?\\d{1,3}(,\\d{3})*(\\.\\d+)?$&#39;<\/p>\n<p>def is_number(string):<\/p>\n<p>    return re.match(pattern, string) is not None<\/p>\n<p>print(is_number(&quot;1,234&quot;))         # True<\/p>\n<p>print(is_number(&quot;-1,234.56&quot;))     # True<\/p>\n<p>print(is_number(&quot;1234,567&quot;))      # False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516d\u3001\u7efc\u5408\u793a\u4f8b<\/h2>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u5224\u65ad\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u6709\u6548\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>import numpy as np<\/p>\n<p>import pandas as pd<\/p>\n<p>from decimal import Decimal, InvalidOperation<\/p>\n<p>def is_number(string):<\/p>\n<p>    # \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u6574\u6570\u548c\u5c0f\u6570<\/p>\n<p>    pattern = r&#39;^-?\\d+(\\.\\d+)?([eE][-+]?\\d+)?$&#39;<\/p>\n<p>    if re.match(pattern, string):<\/p>\n<p>        return True<\/p>\n<p>    # \u4f7f\u7528 int() \u51fd\u6570<\/p>\n<p>    try:<\/p>\n<p>        num = int(string)<\/p>\n<p>        return True<\/p>\n<p>    except ValueError:<\/p>\n<p>        pass<\/p>\n<p>    # \u4f7f\u7528 float() \u51fd\u6570<\/p>\n<p>    try:<\/p>\n<p>        num = float(string)<\/p>\n<p>        return True<\/p>\n<p>    except ValueError:<\/p>\n<p>        pass<\/p>\n<p>    # \u4f7f\u7528 NumPy \u5e93<\/p>\n<p>    try:<\/p>\n<p>        num = np.float64(string)<\/p>\n<p>        return True<\/p>\n<p>    except ValueError:<\/p>\n<p>        pass<\/p>\n<p>    # \u4f7f\u7528 pandas \u5e93<\/p>\n<p>    if pd.to_numeric(string, errors=&#39;coerce&#39;).notna():<\/p>\n<p>        return True<\/p>\n<p>    # \u4f7f\u7528 decimal \u6a21\u5757<\/p>\n<p>    try:<\/p>\n<p>        num = Decimal(string)<\/p>\n<p>        return True<\/p>\n<p>    except InvalidOperation:<\/p>\n<p>        pass<\/p>\n<p>    return False<\/p>\n<p>print(is_number(&quot;123&quot;))         # True<\/p>\n<p>print(is_number(&quot;-123.45&quot;))     # True<\/p>\n<p>print(is_number(&quot;1.23e10&quot;))     # True<\/p>\n<p>print(is_number(&quot;1,234.56&quot;))    # False<\/p>\n<p>print(is_number(&quot;abc123&quot;))      # False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\u548c\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u6709\u6548\u5730\u8bc6\u522b\u5404\u79cd\u683c\u5f0f\u7684\u6570\u5b57\uff0c\u5305\u62ec\u6574\u6570\u3001\u5c0f\u6570\u3001\u8d1f\u6570\u3001\u79d1\u5b66\u8ba1\u6570\u6cd5\u7b49\u3002\u4e0d\u540c\u7684\u65b9\u6cd5\u6709\u4e0d\u540c\u7684\u9002\u7528\u8303\u56f4\u548c\u7279\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u6709\u6548\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u6570\u5b57\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>str.isdigit()<\/code>\u65b9\u6cd5\u6765\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u4ec5\u5305\u542b\u6570\u5b57\u5b57\u7b26\u3002\u4f8b\u5982\uff0c<code>&#39;123&#39;.isdigit()<\/code>\u4f1a\u8fd4\u56de<code>True<\/code>\uff0c\u800c<code>&#39;123a&#39;.isdigit()<\/code>\u5219\u8fd4\u56de<code>False<\/code>\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u7ed3\u6784\u7ed3\u5408<code>int()<\/code>\u6216<code>float()<\/code>\u51fd\u6570\u4e5f\u80fd\u6709\u6548\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u53ef\u4ee5\u8f6c\u6362\u4e3a\u6570\u5b57\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u7528\u4e8e\u6570\u5b57\u8bc6\u522b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5e38\u7528\u7684\u5e93\u5982<code>re<\/code>\uff08\u6b63\u5219\u8868\u8fbe\u5f0f\uff09\u548c<code>numpy<\/code>\u53ef\u4ee5\u5e2e\u52a9\u8bc6\u522b\u548c\u5904\u7406\u6570\u5b57\u3002\u4f7f\u7528<code>re<\/code>\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199\u6a21\u5f0f\u5339\u914d\u6765\u8bc6\u522b\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\uff0c\u4f8b\u5982\u901a\u8fc7<code>re.findall(r&#39;\\d+&#39;, my_string)<\/code>\u53ef\u4ee5\u63d0\u53d6\u6240\u6709\u6570\u5b57\u3002<code>numpy<\/code>\u5219\u63d0\u4f9b\u4e86\u591a\u79cd\u6570\u5b66\u51fd\u6570\uff0c\u9002\u5408\u5904\u7406\u548c\u5206\u6790\u6570\u503c\u6570\u636e\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5305\u542b\u6570\u5b57\u7684\u6df7\u5408\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5728Python\u4e2d\u5904\u7406\u6df7\u5408\u5b57\u7b26\u4e32\uff08\u5373\u540c\u65f6\u5305\u542b\u5b57\u6bcd\u548c\u6570\u5b57\u7684\u5b57\u7b26\u4e32\uff09\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u63d0\u53d6\u6570\u5b57\u90e8\u5206\u3002\u4f8b\u5982\uff0c\u901a\u8fc7<code>re.findall(r&#39;\\d+&#39;, mixed_string)<\/code>\u53ef\u4ee5\u83b7\u53d6\u6240\u6709\u6570\u5b57\uff0c\u8fd4\u56de\u503c\u4f1a\u662f\u4e00\u4e2a\u5217\u8868\u3002\u5982\u679c\u9700\u8981\u5c06\u63d0\u53d6\u7684\u6570\u5b57\u8f6c\u6362\u4e3a\u6574\u6570\u6216\u6d6e\u70b9\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>map()<\/code>\u51fd\u6570\u6765\u5904\u7406\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u8bc6\u522b\u6570\u5b57\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u7c7b\u578b\u8f6c\u6362\u3001\u7b2c\u4e09\u65b9\u5e93\u7b49\u3002\u5176\u4e2d\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305 [&hellip;]","protected":false},"author":3,"featured_media":1181833,"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\/1181822"}],"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=1181822"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1181822\/revisions"}],"predecessor-version":[{"id":1181834,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1181822\/revisions\/1181834"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1181833"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1181822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1181822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1181822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}