{"id":1026455,"date":"2024-12-31T10:42:45","date_gmt":"2024-12-31T02:42:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1026455.html"},"modified":"2024-12-31T10:42:47","modified_gmt":"2024-12-31T02:42:47","slug":"python%e5%a6%82%e4%bd%95%e5%8f%96%e5%87%ba%e6%96%87%e6%9c%ac%e4%b8%ad%e7%9a%84%e6%95%b0%e5%ad%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1026455.html","title":{"rendered":"python\u5982\u4f55\u53d6\u51fa\u6587\u672c\u4e2d\u7684\u6570\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/7044270a-5c23-46aa-8ad0-87dc82c6e93a.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u53d6\u51fa\u6587\u672c\u4e2d\u7684\u6570\u5b57\" \/><\/p>\n<p><p> <strong>Python\u53d6\u51fa\u6587\u672c\u4e2d\u7684\u6570\u5b57\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u5217\u8868\u89e3\u6790\u7b49\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5<\/strong>\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7528\u6765\u5339\u914d\u548c\u63d0\u53d6\u6587\u672c\u4e2d\u7684\u6570\u5b57\uff0c\u64cd\u4f5c\u7b80\u5355\u4e14\u7075\u6d3b\u3002\u6211\u4eec\u63a5\u4e0b\u6765\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff08regex\uff09\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>re<\/code>\u6a21\u5757\u6765\u5b8c\u6210\u8fd9\u4e00\u4efb\u52a1\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u901a\u8fc7\u5339\u914d\u6a21\u5f0f\u6765\u67e5\u627e\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u3002\u4e0b\u9762\u662f\u8be6\u7ec6\u8bf4\u660e\uff1a<\/p>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5339\u914d\u6a21\u5f0f\uff0cPython\u4e2d\u901a\u8fc7<code>re<\/code>\u6a21\u5757\u6765\u64cd\u4f5c\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u9996\u5148\u9700\u8981\u5bfc\u5165<code>re<\/code>\u6a21\u5757\uff0c\u7136\u540e\u7f16\u5199\u5339\u914d\u6570\u5b57\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff0c\u6bd4\u5982<code>\\d+<\/code>\u53ef\u4ee5\u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u8fde\u7eed\u7684\u6570\u5b57\u3002\u4f7f\u7528<code>re.findall()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u63d0\u53d6\u6240\u6709\u7b26\u5408\u6a21\u5f0f\u7684\u5b50\u5b57\u7b26\u4e32\uff0c\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4f1a\u8be6\u7ec6\u8bb2\u89e3\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6587\u672c\u4e2d\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<code>re<\/code>\u6a21\u5757\u662f\u5185\u7f6e\u6a21\u5757\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u989d\u5916\u5b89\u88c5\uff0c\u53ef\u4ee5\u76f4\u63a5\u5bfc\u5165\u4f7f\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u7f16\u5199\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u6a21\u5f0f\u8bed\u8a00\uff0c\u7528\u4e8e\u5339\u914d\u5b57\u7b26\u4e32\u3002<code>\\d<\/code>\u8868\u793a\u6570\u5b57\uff0c<code>+<\/code>\u8868\u793a\u4e00\u4e2a\u6216\u591a\u4e2a\u3002\u7ec4\u5408\u8d77\u6765\uff0c<code>\\d+<\/code>\u8868\u793a\u4e00\u4e2a\u6216\u591a\u4e2a\u8fde\u7eed\u7684\u6570\u5b57\u3002\u8fd9\u4e2a\u6a21\u5f0f\u53ef\u4ee5\u5339\u914d\u6587\u672c\u4e2d\u7684\u6240\u6709\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>re.findall()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>re.findall()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u627e\u5230\u6240\u6709\u7b26\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u7684\u5b50\u5b57\u7b26\u4e32\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;Python 3.8 was released on October 14, 2019. It introduced new features such as assignment expressions.&quot;<\/p>\n<p>pattern = r&#39;\\d+&#39;<\/p>\n<p>numbers = re.findall(pattern, text)<\/p>\n<p>print(numbers)  # Output: [&#39;3&#39;, &#39;8&#39;, &#39;14&#39;, &#39;2019&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u63d0\u53d6\u5230\u7684\u6570\u5b57<\/h3>\n<\/p>\n<p><p>\u63d0\u53d6\u5230\u7684\u6570\u5b57\u9ed8\u8ba4\u662f\u5b57\u7b26\u4e32\u683c\u5f0f\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u6216\u6d6e\u70b9\u6570\u3002\u4ee5\u4e0b\u662f\u8f6c\u6362\u4e3a\u6574\u6570\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [int(num) for num in numbers]<\/p>\n<p>print(numbers)  # Output: [3, 8, 14, 2019]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u6d6e\u70b9\u6570<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u6587\u672c\u4e2d\u5305\u542b\u6d6e\u70b9\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u66f4\u590d\u6742\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u3002\u4f8b\u5982\uff0c\u5339\u914d\u6574\u6570\u548c\u6d6e\u70b9\u6570\u7684\u6a21\u5f0f\u53ef\u4ee5\u662f\uff1a<code>\\d+\\.?\\d*<\/code>\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;The price of the item is 12.99 dollars and the discount is 2.5 percent.&quot;<\/p>\n<p>pattern = r&#39;\\d+\\.?\\d*&#39;<\/p>\n<p>numbers = re.findall(pattern, text)<\/p>\n<p>print(numbers)  # Output: [&#39;12.99&#39;, &#39;2.5&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u7ed3\u5408\u5176\u4ed6\u5b57\u7b26\u4e32\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u63d0\u53d6\u6570\u5b57\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u6709\u65f6\u4e5f\u53ef\u4ee5\u7ed3\u5408\u5176\u4ed6\u5b57\u7b26\u4e32\u65b9\u6cd5\u6765\u5b8c\u6210\u4efb\u52a1\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>str.isdigit()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u9010\u4e2a\u5b57\u7b26\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u6570\u5b57\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;There are 3 cats and 4 dogs in the house.&quot;<\/p>\n<p>numbers = &#39;&#39;.join([char if char.isdigit() else &#39; &#39; for char in text]).split()<\/p>\n<p>numbers = [int(num) for num in numbers]<\/p>\n<p>print(numbers)  # Output: [3, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406\u8d1f\u6570\u548c\u5343\u4f4d\u5206\u9694\u7b26<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5305\u542b\u8d1f\u6570\u548c\u5343\u4f4d\u5206\u9694\u7b26\u7684\u6587\u672c\uff0c\u53ef\u4ee5\u7f16\u5199\u66f4\u590d\u6742\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u3002\u4f8b\u5982\uff0c\u5339\u914d\u5e26\u8d1f\u53f7\u548c\u5343\u4f4d\u5206\u9694\u7b26\u7684\u6570\u5b57\u6a21\u5f0f\u53ef\u4ee5\u662f\uff1a<code>-?\\d{1,3}(,\\d{3})*<\/code>\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;The company&#39;s revenue was -1,234,567 dollars in 2020.&quot;<\/p>\n<p>pattern = r&#39;-?\\d{1,3}(,\\d{3})*&#39;<\/p>\n<p>numbers = re.findall(pattern, text)<\/p>\n<p>print(numbers)  # Output: [&#39;-1,234,567&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6ce8\u610f\uff0c\u4e0a\u8ff0\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u5339\u914d\u7684\u6570\u5b57\u53ef\u80fd\u5305\u542b\u9017\u53f7\uff0c\u9700\u8981\u8fdb\u4e00\u6b65\u5904\u7406\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u5b57\u7c7b\u578b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [int(num.replace(&#39;,&#39;, &#39;&#39;)) for num in numbers]<\/p>\n<p>print(numbers)  # Output: [-1234567]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u7efc\u5408\u5e94\u7528\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4ece\u5305\u542b\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u8d1f\u6570\u548c\u5343\u4f4d\u5206\u9694\u7b26\u7684\u590d\u6742\u6587\u672c\u4e2d\u63d0\u53d6\u6570\u5b57\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;&quot;&quot;<\/p>\n<p>In the fiscal year 2021, the company&#39;s revenue was approximately -1,234,567.89 dollars.<\/p>\n<p>The forecast for 2022 expects a growth of 15.5 percent, reaching around 1,500,000 dollars.<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>pattern = r&#39;-?\\d{1,3}(,\\d{3})*(\\.\\d+)?|-?\\d+(\\.\\d+)?&#39;<\/p>\n<p>numbers = re.findall(pattern, text)<\/p>\n<p>numbers = [num[0] for num in numbers]  # Extract the first group from each match<\/p>\n<p>numbers = [float(num.replace(&#39;,&#39;, &#39;&#39;)) for num in numbers]<\/p>\n<p>print(numbers)<\/p>\n<h2><strong>Output: [-1234567.89, 2021.0, 15.5, 1500000.0]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u63d0\u53d6\u6587\u672c\u4e2d\u7684\u5404\u79cd\u6570\u5b57\uff0c\u5e76\u6839\u636e\u9700\u8981\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u3002\u8fd9\u4e9b\u65b9\u6cd5\u9002\u7528\u4e8e\u4e0d\u540c\u573a\u666f\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u63d0\u53d6\u6587\u672c\u4e2d\u7684\u6240\u6709\u6570\u5b57\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u63d0\u53d6\u6587\u672c\u4e2d\u7684\u6240\u6709\u6570\u5b57\u3002\u901a\u8fc7<code>re<\/code>\u6a21\u5757\u4e2d\u7684<code>findall<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>re.findall(r&#39;\\d+&#39;, text)<\/code>\u53ef\u4ee5\u8fd4\u56de\u6587\u672c\u4e2d\u6240\u6709\u7684\u6570\u5b57\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>\u63d0\u53d6\u6d6e\u70b9\u6570\u548c\u6574\u6570\u6709\u4ec0\u4e48\u4e0d\u540c\u7684\u65b9\u6cd5\u5417\uff1f<\/strong><br \/>\u5f53\u7136\uff0c\u63d0\u53d6\u6574\u6570\u548c\u6d6e\u70b9\u6570\u7684\u65b9\u6cd5\u6709\u6240\u4e0d\u540c\u3002\u5982\u679c\u60f3\u8981\u63d0\u53d6\u6d6e\u70b9\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<code>r&#39;\\d+\\.?\\d*&#39;<\/code>\uff0c\u8fd9\u5c06\u5339\u914d\u6574\u6570\u548c\u5c0f\u6570\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c<code>re.findall(r&#39;\\d+\\.?\\d*&#39;, text)<\/code>\u4f1a\u8fd4\u56de\u6587\u672c\u4e2d\u7684\u6240\u6709\u6574\u6570\u548c\u6d6e\u70b9\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5305\u542b\u683c\u5f0f\u5316\u6570\u5b57\u7684\u6587\u672c\uff0c\u6bd4\u5982\u5e26\u6709\u5343\u5206\u4f4d\u7b26\u7684\u6570\u5b57\uff1f<\/strong><br \/>\u5bf9\u4e8e\u5e26\u6709\u5343\u5206\u4f4d\u7b26\u7684\u6570\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528\u66f4\u590d\u6742\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u4f8b\u5982\uff0c<code>r&#39;\\d{1,3}(?:,\\d{3})*(?:\\.\\d+)?&#39;<\/code>\u80fd\u591f\u5339\u914d\u5f62\u5982\u201c1,234.56\u201d\u7684\u683c\u5f0f\u3002\u5728\u63d0\u53d6\u540e\uff0c\u53ef\u80fd\u8fd8\u9700\u8981\u7528<code>replace<\/code>\u65b9\u6cd5\u5c06\u9017\u53f7\u53bb\u6389\uff0c\u4ee5\u4fbf\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\u7c7b\u578b\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u63d0\u53d6\u7684\u6570\u5b57\u8f6c\u6362\u4e3a\u6574\u6570\u6216\u6d6e\u70b9\u6570\uff1f<\/strong><br \/>\u63d0\u53d6\u6570\u5b57\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>int()<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u6216\u4f7f\u7528<code>float()<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002\u786e\u4fdd\u5728\u8f6c\u6362\u4e4b\u524d\u53bb\u6389\u4efb\u4f55\u4e0d\u5fc5\u8981\u7684\u5b57\u7b26\uff0c\u5982\u7a7a\u683c\u6216\u5343\u5206\u4f4d\u7b26\uff0c\u4ee5\u907f\u514d\u8f6c\u6362\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53d6\u51fa\u6587\u672c\u4e2d\u7684\u6570\u5b57\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u5217\u8868\u89e3\u6790\u7b49\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u6b63\u5219\u8868\u8fbe [&hellip;]","protected":false},"author":3,"featured_media":1026461,"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\/1026455"}],"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=1026455"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1026455\/revisions"}],"predecessor-version":[{"id":1026464,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1026455\/revisions\/1026464"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1026461"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1026455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1026455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1026455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}