{"id":177145,"date":"2024-05-08T19:24:58","date_gmt":"2024-05-08T11:24:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/177145.html"},"modified":"2024-05-08T19:25:03","modified_gmt":"2024-05-08T11:25:03","slug":"python%e6%80%8e%e4%b9%88%e6%8a%8a%e5%90%ab%e6%9c%89%e5%85%b3%e9%94%ae%e8%af%8d%e7%9a%84%e5%8f%a5%e5%ad%90%ef%bc%8c%e4%bb%8e%e6%96%87%e5%ad%97%e4%b8%b2%e9%87%8c%e6%8f%90%e5%8f%96%e5%87%ba%e6%9d%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/177145.html","title":{"rendered":"python\u600e\u4e48\u628a\u542b\u6709\u5173\u952e\u8bcd\u7684\u53e5\u5b50\uff0c\u4ece\u6587\u5b57\u4e32\u91cc\u63d0\u53d6\u51fa\u6765"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27061035\/56ffa273-9d80-4444-bb0a-7aad9d88df6e.webp\" alt=\"python\u600e\u4e48\u628a\u542b\u6709\u5173\u952e\u8bcd\u7684\u53e5\u5b50\uff0c\u4ece\u6587\u5b57\u4e32\u91cc\u63d0\u53d6\u51fa\u6765\" \/><\/p>\n<p><p>\u8981\u4ece\u4e00\u4e2a\u8f83\u957f\u7684\u6587\u672c\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u5305\u542b\u7279\u5b9a\u5173\u952e\u8bcd\u7684\u53e5\u5b50\uff0c\u4e3b\u8981\u65b9\u6cd5\u6709\u4e24\u79cd\uff1a<strong>\u4f7f\u7528Python\u6807\u51c6\u5e93\u4e2d\u7684\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5229\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\u3002\u5728\u8fd9\u4e24\u79cd\u65b9\u6cd5\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u4f9b\u4e86\u66f4\u52a0\u5f3a\u5927\u548c\u7075\u6d3b\u7684\u6587\u672c\u5339\u914d\u548c\u641c\u7d22\u529f\u80fd\uff0c\u662f\u5904\u7406\u8fd9\u7c7b\u95ee\u9898\u7684\u9996\u9009\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e00\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5206\u5272\u5b57\u7b26\u4e32\u3001\u904d\u5386\u53e5\u5b50\uff0c\u7136\u540e\u68c0\u67e5\u53e5\u5b50\u4e2d\u662f\u5426\u5305\u542b\u6307\u5b9a\u7684\u5173\u952e\u8bcd\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u7b80\u5355\uff0c\u4f46\u5bf9\u4e8e\u590d\u6742\u7684\u6587\u672c\u6570\u636e\u53ef\u80fd\u4e0d\u591f\u9ad8\u6548\u6216\u51c6\u786e\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5c06\u6587\u672c\u5b57\u7b26\u4e32\u5206\u5272\u6210\u5355\u72ec\u7684\u53e5\u5b50\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u8bc6\u522b\u53e5\u5b50\u7684\u7ed3\u675f\u6807\u8bc6\u7b26\uff08\u5982\u53e5\u53f7\u3001\u95ee\u53f7\u548c\u611f\u53f9\u53f7\uff09\u6765\u5b9e\u73b0\u3002\u7136\u540e\uff0c\u904d\u5386\u8fd9\u4e9b\u53e5\u5b50\uff0c\u68c0\u67e5\u6bcf\u4e2a\u53e5\u5b50\u662f\u5426\u5305\u542b\u6211\u4eec\u60f3\u8981\u7684\u5173\u952e\u8bcd\u3002\u5982\u679c\u5305\u542b\uff0c\u5c31\u5c06\u8fd9\u4e2a\u53e5\u5b50\u4fdd\u5b58\u6216\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><\/p>\n<\/p>\n<p><p><strong>\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u9ad8\u6548\u548c\u7cbe\u51c6\u7684\u65b9\u5f0f\u6765\u5904\u7406\u6587\u672c\u3002\u901a\u8fc7\u5b9a\u4e49\u5177\u4f53\u7684\u6a21\u5f0f\u5339\u914d\u89c4\u5219\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u63d0\u53d6\u5305\u542b\u6307\u5b9a\u5173\u952e\u8bcd\u7684\u53e5\u5b50\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u5904\u7406\u5305\u542b\u590d\u6742\u7ed3\u6784\u548c\u4e0d\u540c\u6807\u70b9\u7b26\u53f7\u7684\u957f\u6587\u672c\u65f6\u5c24\u5176\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u53e5\u5b50\u65f6\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e24\u4e2a\u4e3b\u8981\u90e8\u5206\uff1a\u4e00\u90e8\u5206\u662f\u5339\u914d\u53e5\u5b50\u8fb9\u754c\u7684\u89c4\u5219\uff0c\u53e6\u4e00\u90e8\u5206\u662f\u5339\u914d\u5173\u952e\u8bcd\u7684\u89c4\u5219\u3002\u901a\u8fc7\u7ed3\u5408\u8fd9\u4e24\u90e8\u5206\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u7cbe\u786e\u5730\u5b9a\u4f4d\u5e76\u63d0\u53d6\u5305\u542b\u7279\u5b9a\u5173\u952e\u8bcd\u7684\u53e5\u5b50\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5e94\u7528\u8fd9\u4e24\u79cd\u65b9\u6cd5\u6765\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><strong>1. \u5206\u5272\u6587\u672c\u4e3a\u53e5\u5b50<\/strong><\/p>\n<\/p>\n<p><p>Python\u4e2d\u53ef\u4ee5\u4f7f\u7528<code>split<\/code>\u65b9\u6cd5\u6309\u7167\u53e5\u5b50\u7684\u7ed3\u675f\u6807\u5fd7\u5206\u5272\u6587\u672c\u3002\u7ed3\u675f\u6807\u5fd7\u901a\u5e38\u5305\u62ec\u53e5\u53f7\uff08<code>.<\/code>\uff09\u3001\u95ee\u53f7\uff08<code>?<\/code>\uff09\u548c\u611f\u53f9\u53f7\uff08<code>!<\/code>\uff09\u3002\u4f46\u662f\uff0c\u8fd9\u79cd\u65b9\u6cd5\u53ef\u80fd\u9700\u8981\u8fdb\u4e00\u6b65\u5904\u7406\uff0c\u4ee5\u9002\u5e94\u5305\u542b\u7f29\u5199\u3001\u65e5\u671f\u7b49\u7279\u6b8a\u60c5\u51b5\u7684\u6587\u672c\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u68c0\u67e5\u53e5\u5b50\u662f\u5426\u5305\u542b\u5173\u952e\u8bcd<\/strong><\/p>\n<\/p>\n<p><p>\u5206\u5272\u6210\u53e5\u5b50\u540e\uff0c\u904d\u5386\u6bcf\u4e2a\u53e5\u5b50\uff0c\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u68c0\u67e5\u53e5\u5b50\u662f\u5426\u5305\u542b\u76ee\u6807\u5173\u952e\u8bcd\u3002\u5982\u679c\u5305\u542b\uff0c\u5219\u4fdd\u7559\u6216\u6253\u5370\u8be5\u53e5\u5b50\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u76f4\u622a\u4e86\u5f53\uff0c\u4f46\u662f\u53ef\u80fd\u4e0d\u591f\u7075\u6d3b\uff0c\u5c24\u5176\u662f\u5728\u5173\u952e\u8bcd\u6709\u591a\u79cd\u5f62\u5f0f\u6216\u8005\u9700\u8981\u8003\u8651\u5927\u5c0f\u5199\u65f6\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p><strong>1. \u5b9a\u4e49\u5339\u914d\u6a21\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u65f6\uff0c\u9996\u5148\u9700\u8981\u5b9a\u4e49\u5339\u914d\u53e5\u5b50\u7684\u6a21\u5f0f\u3002\u8fd9\u901a\u5e38\u6d89\u53ca\u5230\u754c\u5b9a\u53e5\u5b50\u7684\u8fb9\u7f18\uff0c\u5e76\u6b63\u786e\u5904\u7406\u6807\u70b9\u7b26\u53f7\u548c\u5176\u4ed6\u7279\u6b8a\u5b57\u7b26\u3002Python\u7684<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\u6765\u6784\u5efa\u8fd9\u4e9b\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u63d0\u53d6\u5305\u542b\u5173\u952e\u8bcd\u7684\u53e5\u5b50<\/strong><\/p>\n<\/p>\n<p><p>\u6784\u5efa\u597d\u5339\u914d\u6a21\u5f0f\u4e4b\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>re.findall<\/code>\u6216<code>re.finditer<\/code>\u65b9\u6cd5\u6765\u641c\u7d22\u548c\u63d0\u53d6\u7b26\u5408\u6761\u4ef6\u7684\u53e5\u5b50\u3002\u8fd9\u4e9b\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5339\u914d\u7ed3\u679c\u7684\u5217\u8868\uff0c\u5176\u4e2d\u6bcf\u4e2a\u7ed3\u679c\u90fd\u662f\u5305\u542b\u6307\u5b9a\u5173\u952e\u8bcd\u7684\u5b8c\u6574\u53e5\u5b50\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b9e\u65bd\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5177\u4f53\u793a\u4f8b<\/strong><\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u60f3\u8981\u4ece\u4e00\u6bb5\u6587\u672c\u4e2d\u63d0\u53d6\u6240\u6709\u5305\u542b\u5173\u952e\u8bcd\u201cPython\u201d\u7684\u53e5\u5b50\u3002\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5c06\u201cPython\u201d\u4f5c\u4e3a\u5173\u952e\u8bcd\uff0c\u5e76\u56f4\u7ed5\u5b83\u5339\u914d\u4efb\u610f\u6570\u91cf\u7684\u975e\u53e5\u53f7\u5b57\u7b26\uff08\u8fd9\u91cc\u5047\u5b9a\u53e5\u5b50\u7531\u53e5\u53f7\u7ed3\u675f\uff09\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;\u8fd9\u662f\u4e00\u4e2a\u4f8b\u5b50\u3002Python\u662f\u4e00\u95e8\u5f3a\u5927\u7684\u7f16\u7a0b\u8bed\u8a00\u3002\u5b83\u5e7f\u6cdb\u7528\u4e8e\u6570\u636e\u5206\u6790\u3001Web\u5f00\u53d1\u7b49\u9886\u57df\u3002&quot;<\/p>\n<h2><strong>\u5b9a\u4e49\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f<\/strong><\/h2>\n<p>pattern = r&#039;[^\u3002]*?Python[^\u3002]*\u3002&#039;<\/p>\n<h2><strong>\u4f7f\u7528re.findall\u65b9\u6cd5\u67e5\u627e\u6240\u6709\u5339\u914d\u9879<\/strong><\/h2>\n<p>sentences = re.findall(pattern, text)<\/p>\n<p>for sentence in sentences:<\/p>\n<p>    print(sentence)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f<code>[^\u3002]*?Python[^\u3002]*\u3002<\/code>\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6a21\u5f0f\uff0c\u8be5\u6a21\u5f0f\u5339\u914d\u4efb\u610f\u5305\u542b\u201cPython\u201d\u7684\u53e5\u5b50\u3002\u8fd9\u91cc\u7684<code>[^\u3002]<\/code>\u8868\u793a\u5339\u914d\u9664\u4e86\u53e5\u53f7\u5916\u7684\u4efb\u4f55\u5b57\u7b26\uff0c\u786e\u4fdd\u6211\u4eec\u6b63\u786e\u5730\u63d0\u53d6\u6574\u53e5\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u91c7\u7528\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u6211\u4eec\u80fd\u591f\u6709\u6548\u5730\u4ece\u5927\u91cf\u6587\u672c\u4e2d\u63d0\u53d6\u5305\u542b\u7279\u5b9a\u5173\u952e\u8bcd\u7684\u53e5\u5b50\uff0c\u4e0d\u8bba\u662f\u4f7f\u7528\u57fa\u672c\u7684\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5\uff0c\u8fd8\u662f\u5229\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5f3a\u5927\u529f\u80fd\u3002\u5bf9\u4e8e\u5904\u7406\u590d\u6742\u7684\u6587\u672c\u6570\u636e\u800c\u8a00\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u5c24\u5176\u6709\u4ef7\u503c\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u7075\u6d3b\u6027\u548c\u7cbe\u786e\u5ea6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u4f7f\u7528Python\u4ece\u6587\u672c\u4e32\u4e2d\u63d0\u53d6\u5305\u542b\u5173\u952e\u8bcd\u7684\u53e5\u5b50\uff1f<\/strong><br \/>\n\u8981\u4ece\u5305\u542b\u5173\u952e\u8bcd\u7684\u6587\u5b57\u4e32\u4e2d\u63d0\u53d6\u51fa\u76f8\u5173\u53e5\u5b50\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Python\u7684\u5b57\u7b26\u4e32\u51fd\u6570\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u5b8c\u6210\u3002\u60a8\u53ef\u4ee5\u9996\u5148\u5c06\u6587\u672c\u4e32\u5206\u5272\u4e3a\u53e5\u5b50\uff0c\u5e76\u4f7f\u7528\u5b57\u7b26\u4e32\u51fd\u6570\u6765\u68c0\u67e5\u53e5\u5b50\u4e2d\u662f\u5426\u5305\u542b\u5173\u952e\u8bcd\u3002\u5982\u679c\u53e5\u5b50\u4e2d\u5305\u542b\u5173\u952e\u8bcd\uff0c\u60a8\u53ef\u4ee5\u5c06\u5176\u4fdd\u5b58\u5230\u7ed3\u679c\u4e2d\u3002<\/p>\n<p><strong>2. Python\u4e2d\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u8fc7\u6ee4\u6389\u4e0d\u5305\u542b\u5173\u952e\u8bcd\u7684\u53e5\u5b50\uff1f<\/strong><br \/>\n\u8981\u8fc7\u6ee4\u6389\u4e0d\u5305\u542b\u5173\u952e\u8bcd\u7684\u53e5\u5b50\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5757re\u3002\u901a\u8fc7\u4f7f\u7528re\u6a21\u5757\u7684match\u6216search\u51fd\u6570\uff0c\u60a8\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u5305\u542b\u5173\u952e\u8bcd\u7684\u53e5\u5b50\u3002\u5982\u679c\u5339\u914d\u6210\u529f\uff0c\u60a8\u53ef\u4ee5\u5c06\u8be5\u53e5\u5b50\u4fdd\u5b58\u5230\u7ed3\u679c\u4e2d\u3002<\/p>\n<p><strong>3. \u4f7f\u7528Python\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u63d0\u53d6\u542b\u6709\u5173\u952e\u8bcd\u7684\u53e5\u5b50\u6709\u4ec0\u4e48\u6280\u5de7\uff1f<\/strong><br \/>\n\u8981\u7f16\u5199\u4e00\u4e2a\u53ef\u4ee5\u63d0\u53d6\u5305\u542b\u5173\u952e\u8bcd\u7684\u53e5\u5b50\u7684\u51fd\u6570\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5b57\u7b26\u4e32\u51fd\u6570\u4ee5\u53ca\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u60a8\u53ef\u4ee5\u5229\u7528\u5b57\u7b26\u4e32\u7684split\u51fd\u6570\u5c06\u6587\u672c\u4e32\u5206\u5272\u4e3a\u53e5\u5b50\uff0c\u7136\u540e\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u68c0\u67e5\u6bcf\u4e2a\u53e5\u5b50\u4e2d\u662f\u5426\u5b58\u5728\u5173\u952e\u8bcd\u3002\u5982\u679c\u5b58\u5728\u5173\u952e\u8bcd\uff0c\u60a8\u53ef\u4ee5\u5c06\u8be5\u53e5\u5b50\u4fdd\u5b58\u5230\u7ed3\u679c\u5217\u8868\u4e2d\u3002\u6b64\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u7b80\u5316\u4ee3\u7801\uff0c\u4f7f\u5176\u66f4\u52a0\u7b80\u6d01\u9ad8\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u4ece\u4e00\u4e2a\u8f83\u957f\u7684\u6587\u672c\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u5305\u542b\u7279\u5b9a\u5173\u952e\u8bcd\u7684\u53e5\u5b50\uff0c\u4e3b\u8981\u65b9\u6cd5\u6709\u4e24\u79cd\uff1a\u4f7f\u7528Python\u6807\u51c6\u5e93\u4e2d\u7684\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5\uff0c [&hellip;]","protected":false},"author":3,"featured_media":177153,"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\/177145"}],"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=177145"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/177145\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/177153"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=177145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=177145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=177145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}