{"id":1100484,"date":"2025-01-08T15:40:22","date_gmt":"2025-01-08T07:40:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1100484.html"},"modified":"2025-01-08T15:40:25","modified_gmt":"2025-01-08T07:40:25","slug":"python%e5%a6%82%e4%bd%95%e5%8f%96%e6%9f%90%e5%ad%97%e7%ac%a6%e5%89%8d%e9%9d%a2%e7%9a%84%e6%95%b0%e5%80%bc-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1100484.html","title":{"rendered":"python\u5982\u4f55\u53d6\u67d0\u5b57\u7b26\u524d\u9762\u7684\u6570\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063721\/1d0927dd-e866-4a80-95fd-709934939a19.webp\" alt=\"python\u5982\u4f55\u53d6\u67d0\u5b57\u7b26\u524d\u9762\u7684\u6570\u503c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6765\u83b7\u53d6\u67d0\u4e2a\u5b57\u7b26\u524d\u9762\u7684\u6570\u503c<\/strong>\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5b57\u7b26\u4e32\u5207\u7247\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u4ee5\u53ca\u5185\u7f6e\u51fd\u6570\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4e00\u79cd\u5e38\u7528\u7684\u65b9\u5f0f\uff1a<strong>\u5b57\u7b26\u4e32\u5207\u7247<\/strong>\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b57\u7b26\u4e32\u5207\u7247\u662f\u4e00\u79cd\u975e\u5e38\u6709\u6548\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6765\u83b7\u53d6\u7279\u5b9a\u5b57\u7b26\u524d\u9762\u7684\u6570\u503c<\/strong>\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32<code>&quot;123abc456&quot;<\/code>\uff0c\u6211\u4eec\u8981\u83b7\u53d6\u5b57\u7b26<code>&quot;a&quot;<\/code>\u524d\u9762\u7684\u6570\u503c<code>&quot;123&quot;<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u5207\u7247\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u51e0\u79cd\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\u6765\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b57\u7b26\u4e32\u5207\u7247<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u5207\u7247\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7\u5207\u7247\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u67d0\u4e00\u90e8\u5206\u3002\u5047\u8bbe\u6211\u4eec\u8981\u83b7\u53d6\u5b57\u7b26<code>&quot;a&quot;<\/code>\u524d\u9762\u7684\u6570\u503c\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u627e\u5230\u5b57\u7b26<code>&quot;a&quot;<\/code>\u7684\u4f4d\u7f6e\uff0c\u7136\u540e\u4f7f\u7528\u5207\u7247\u6765\u83b7\u53d6\u5176\u524d\u9762\u7684\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;123abc456&quot;<\/p>\n<p>char = &#39;a&#39;<\/p>\n<p>index = string.find(char)<\/p>\n<p>if index != -1:  # \u68c0\u67e5\u5b57\u7b26\u662f\u5426\u5b58\u5728<\/p>\n<p>    num_str = string[:index]<\/p>\n<p>    print(num_str)  # \u8f93\u51fa\uff1a123<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u5b57\u7b26\u4e0d\u5b58\u5728&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u4f7f\u7528<code>find<\/code>\u65b9\u6cd5\u627e\u5230\u5b57\u7b26<code>&quot;a&quot;<\/code>\u7684\u4f4d\u7f6e\uff0c\u7136\u540e\u4f7f\u7528\u5207\u7247\u64cd\u4f5c<code>string[:index]<\/code>\u83b7\u53d6\u5176\u524d\u9762\u7684\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u5339\u914d\u5de5\u5177\uff0c\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u627e\u5230\u7279\u5b9a\u6a21\u5f0f\u7684\u5b57\u7b26\u4e32\u3002\u5047\u8bbe\u6211\u4eec\u8981\u83b7\u53d6\u5b57\u7b26<code>&quot;a&quot;<\/code>\u524d\u9762\u7684\u6570\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>string = &quot;123abc456&quot;<\/p>\n<p>pattern = r&#39;(\\d+)a&#39;<\/p>\n<p>match = re.search(pattern, string)<\/p>\n<p>if match:<\/p>\n<p>    num_str = match.group(1)<\/p>\n<p>    print(num_str)  # \u8f93\u51fa\uff1a123<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u5339\u914d\u5931\u8d25&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>pattern<\/code>\u8868\u793a\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5176\u4e2d<code>(\\d+)<\/code>\u8868\u793a\u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u6570\u5b57\uff0c<code>a<\/code>\u8868\u793a\u5339\u914d\u5b57\u7b26<code>&quot;a&quot;<\/code>\u3002\u4f7f\u7528<code>re.search<\/code>\u65b9\u6cd5\u53ef\u4ee5\u627e\u5230\u5339\u914d\u7684\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u901a\u8fc7<code>group<\/code>\u65b9\u6cd5\u83b7\u53d6\u5339\u914d\u7684\u6570\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5185\u7f6e\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u51fd\u6570\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u65b9\u4fbf\u5730\u5904\u7406\u5b57\u7b26\u4e32\u3002\u6bd4\u5982\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u89e3\u6790\u548c<code>isdigit<\/code>\u65b9\u6cd5\u6765\u8fc7\u6ee4\u51fa\u6570\u5b57\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;123abc456&quot;<\/p>\n<p>num_str = &#39;&#39;.join([char for char in string if char.isdigit()])<\/p>\n<p>print(num_str)  # \u8f93\u51fa\uff1a123456<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528\u5217\u8868\u89e3\u6790\u548c<code>isdigit<\/code>\u65b9\u6cd5\u8fc7\u6ee4\u51fa\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5206\u5272\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>split<\/code>\u65b9\u6cd5\uff0c\u5c06\u5b57\u7b26\u4e32\u6309\u7167\u7279\u5b9a\u5b57\u7b26\u5206\u5272\uff0c\u7136\u540e\u83b7\u53d6\u5206\u5272\u540e\u7684\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string = &quot;123abc456&quot;<\/p>\n<p>char = &#39;a&#39;<\/p>\n<p>parts = string.split(char)<\/p>\n<p>if len(parts) &gt; 1:<\/p>\n<p>    num_str = parts[0]<\/p>\n<p>    print(num_str)  # \u8f93\u51fa\uff1a123<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u5b57\u7b26\u4e0d\u5b58\u5728&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528<code>split<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u6309\u7167\u5b57\u7b26<code>&quot;a&quot;<\/code>\u5206\u5272\uff0c\u7136\u540e\u83b7\u53d6\u5206\u5272\u540e\u7684\u7b2c\u4e00\u4e2a\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u83b7\u53d6\u67d0\u4e2a\u5b57\u7b26\u524d\u9762\u7684\u6570\u503c\u3002\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u548c\u5b57\u7b26\u4e32\u7684\u590d\u6742\u7a0b\u5ea6\u6765\u51b3\u5b9a\u3002\u5b57\u7b26\u4e32\u5207\u7247\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u6700\u5e38\u7528\u7684\u4e24\u79cd\u65b9\u6cd5\uff0c\u524d\u8005\u9002\u7528\u4e8e\u7b80\u5355\u60c5\u51b5\uff0c\u540e\u8005\u9002\u7528\u4e8e\u590d\u6742\u6a21\u5f0f\u5339\u914d\u3002\u5185\u7f6e\u51fd\u6570\u548c\u5b57\u7b26\u4e32\u5206\u5272\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u5728\u7279\u5b9a\u573a\u666f\u4e0b\u63d0\u4f9b\u4fbf\u5229\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u786e\u4fdd\u4ee3\u7801\u7684\u5065\u58ee\u6027\uff0c\u5904\u7406\u597d\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u60c5\u51b5\uff0c\u6bd4\u5982\u5b57\u7b26\u4e0d\u5b58\u5728\u6216\u5b57\u7b26\u4e32\u683c\u5f0f\u4e0d\u7b26\u5408\u9884\u671f\u7b49\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u63d0\u53d6\u7279\u5b9a\u5b57\u7b26\u524d\u7684\u6570\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u63d0\u53d6\u7279\u5b9a\u5b57\u7b26\u524d\u7684\u6570\u503c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>str.split()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8f7b\u677e\u5206\u5272\u5b57\u7b26\u4e32\u5e76\u83b7\u53d6\u6240\u9700\u90e8\u5206\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">text = &quot;\u4ef7\u683c\u662f100\u5143&quot;\nnumber = text.split(&quot;\u662f&quot;)[1].split(&quot;\u5143&quot;)[0]\nprint(number)  # \u8f93\u51fa\uff1a100\n<\/code><\/pre>\n<p>\u6b64\u5916\uff0c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b\u5730\u5339\u914d\u590d\u6742\u7684\u5b57\u7b26\u4e32\u6a21\u5f0f\u3002\u53ef\u4ee5\u5bfc\u5165<code>re<\/code>\u6a21\u5757\uff0c\u4f7f\u7528<code>re.search()<\/code>\u51fd\u6570\u6765\u63d0\u53d6\u6240\u9700\u6570\u503c\u3002<\/p>\n<p><strong>\u5728\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e\u7279\u5b9a\u5b57\u7b26\u7684\u4f4d\u7f6e\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u65b9\u6cd5\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c<code>str.find()<\/code>\u548c<code>str.index()<\/code>\u65b9\u6cd5\u90fd\u53ef\u4ee5\u7528\u6765\u67e5\u627e\u7279\u5b9a\u5b57\u7b26\u7684\u4f4d\u7f6e\u3002<code>str.find()<\/code>\u8fd4\u56de\u5b57\u7b26\u9996\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\uff0c\u5982\u679c\u672a\u627e\u5230\u5219\u8fd4\u56de-1\uff1b\u800c<code>str.index()<\/code>\u5219\u4f1a\u5728\u672a\u627e\u5230\u65f6\u629b\u51fa\u5f02\u5e38\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u51c6\u786e\u83b7\u53d6\u5b57\u7b26\u7684\u7d22\u5f15\uff0c\u4ece\u800c\u63d0\u53d6\u8be5\u5b57\u7b26\u524d\u7684\u6570\u503c\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u60f3\u63d0\u53d6\u591a\u4e2a\u6570\u5b57\uff0c\u5e94\u8be5\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u82e5\u60f3\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u6570\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7684<code>re.findall()<\/code>\u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u8fd4\u56de\u6240\u6709\u5339\u914d\u7684\u7ed3\u679c\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import re\n\ntext = &quot;\u57282021\u5e74\u67091000\u4ef6\u5546\u54c1\uff0c2022\u5e74\u67091500\u4ef6\u5546\u54c1\u3002&quot;\nnumbers = re.findall(r&#39;\\d+&#39;, text)\nprint(numbers)  # \u8f93\u51fa\uff1a[&#39;2021&#39;, &#39;1000&#39;, &#39;2022&#39;, &#39;1500&#39;]\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u7684\u6570\u5b57\uff0c\u65b9\u4fbf\u540e\u7eed\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6765\u83b7\u53d6\u67d0\u4e2a\u5b57\u7b26\u524d\u9762\u7684\u6570\u503c\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5b57\u7b26\u4e32\u5207\u7247\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u4ee5\u53ca\u5185\u7f6e\u51fd [&hellip;]","protected":false},"author":3,"featured_media":1100488,"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\/1100484"}],"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=1100484"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1100484\/revisions"}],"predecessor-version":[{"id":1100491,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1100484\/revisions\/1100491"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1100488"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1100484"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1100484"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1100484"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}