{"id":980663,"date":"2024-12-27T06:56:12","date_gmt":"2024-12-26T22:56:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/980663.html"},"modified":"2024-12-27T06:56:14","modified_gmt":"2024-12-26T22:56:14","slug":"%e7%94%a8python%e5%a6%82%e4%bd%95%e5%8e%bb%e6%8e%89%e6%96%87%e5%ad%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/980663.html","title":{"rendered":"\u7528python\u5982\u4f55\u53bb\u6389\u6587\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24205942\/7113f06e-7f5a-4cb3-a7ea-b2b6613b12b7.webp\" alt=\"\u7528python\u5982\u4f55\u53bb\u6389\u6587\u5b57\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u53bb\u6389\u6587\u5b57\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5b57\u7b26\u4e32\u66ff\u6362\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u6587\u672c\u5904\u7406\u5e93\uff08\u5982nltk\uff09\u3002<\/strong>\u5176\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u6700\u4e3a\u7075\u6d3b\u548c\u5f3a\u5927\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u5339\u914d\u590d\u6742\u7684\u6587\u672c\u6a21\u5f0f\u5e76\u8fdb\u884c\u66ff\u6362\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u53bb\u6389\u7279\u5b9a\u7684\u6587\u5b57\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u66ff\u6362<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u66ff\u6362\u662fPython\u4e2d\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7\u5b57\u7b26\u4e32\u7684<code>replace()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u66ff\u6362\u6216\u53bb\u9664\u6307\u5b9a\u7684\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, this is a sample text.&quot;<\/p>\n<p>cleaned_text = text.replace(&quot;sample&quot;, &quot;&quot;)<\/p>\n<p>print(cleaned_text)  # \u8f93\u51fa: Hello, this is a  text.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>replace()<\/code>\u65b9\u6cd5\u5c06&quot;sample&quot;\u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32\uff0c\u4ece\u800c\u53bb\u6389\u4e86\u5b83\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2<\/strong>\uff0c\u4f46\u4ec5\u9002\u7528\u4e8e\u5df2\u77e5\u7684\u7b80\u5355\u6587\u672c\u66ff\u6362\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\uff08regular expressions\uff09\u662f\u4e00\u79cd\u975e\u5e38\u5f3a\u5927\u7684\u6587\u672c\u5904\u7406\u5de5\u5177\uff0c\u5c24\u5176\u5728\u9700\u8981\u5904\u7406\u590d\u6742\u7684\u5b57\u7b26\u4e32\u5339\u914d\u548c\u66ff\u6362\u65f6\u3002Python\u63d0\u4f9b\u4e86<code>re<\/code>\u6a21\u5757\u6765\u652f\u6301\u6b63\u5219\u8868\u8fbe\u5f0f\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u4f7f\u7528\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>re.sub()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u66ff\u6362\u5339\u914d\u7684\u6587\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;Hello, this is a sample text with numbers 12345.&quot;<\/p>\n<p>cleaned_text = re.sub(r&#39;\\d+&#39;, &#39;&#39;, text)<\/p>\n<p>print(cleaned_text)  # \u8f93\u51fa: Hello, this is a sample text with numbers .<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>\\d+<\/code>\u662f\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff0c\u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u6570\u5b57\u5b57\u7b26\u3002<code>re.sub()<\/code>\u51fd\u6570\u5c06\u6240\u6709\u5339\u914d\u7684\u5185\u5bb9\u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32\uff0c\u4ece\u800c\u53bb\u6389\u4e86\u6587\u672c\u4e2d\u7684\u6240\u6709\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h4>2. \u53bb\u6389\u7279\u5b9a\u7684\u6a21\u5f0f<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u8981\u53bb\u6389\u6587\u672c\u4e2d\u7684\u6240\u6709HTML\u6807\u7b7e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;&lt;p&gt;Hello, &lt;b&gt;this&lt;\/b&gt; is a &lt;a href=&#39;#&#39;&gt;link&lt;\/a&gt;.&lt;\/p&gt;&quot;<\/p>\n<p>cleaned_text = re.sub(r&#39;&lt;.*?&gt;&#39;, &#39;&#39;, text)<\/p>\n<p>print(cleaned_text)  # \u8f93\u51fa: Hello, this is a link.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>&lt;.*?&gt;<\/code>\u662f\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff0c\u7528\u4e8e\u5339\u914dHTML\u6807\u7b7e\u3002<code>.*?<\/code>\u4e2d\u7684\u95ee\u53f7\u4f7f\u5f97\u5339\u914d\u662f\u975e\u8d2a\u5a6a\u7684\uff0c\u5373\u5c3d\u53ef\u80fd\u5c11\u5730\u5339\u914d\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><p><strong>\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7075\u6d3b\u6027<\/strong>\u4f7f\u5176\u9002\u7528\u4e8e\u5404\u79cd\u590d\u6742\u7684\u6587\u672c\u5904\u7406\u4efb\u52a1\uff0c\u7279\u522b\u662f\u5f53\u9700\u8981\u5339\u914d\u548c\u53bb\u9664\u7684\u6a21\u5f0f\u4e0d\u56fa\u5b9a\u6216\u6bd4\u8f83\u590d\u6742\u65f6\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u6587\u672c\u5904\u7406\u5e93<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u6709\u591a\u4e2a\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u7528\u4e8e\u6587\u672c\u5904\u7406\uff0c\u4f8b\u5982nltk\u3001spacy\u7b49\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u6587\u672c\u5206\u6790\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u4e8e\u53bb\u9664\u7279\u5b9a\u7c7b\u578b\u7684\u6587\u672c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528nltk\u53bb\u9664\u505c\u7528\u8bcd<\/h4>\n<\/p>\n<p><p>\u505c\u7528\u8bcd\u662f\u6307\u5728\u6587\u672c\u5904\u7406\u4e2d\u65e0\u9700\u5173\u6ce8\u7684\u9ad8\u9891\u6b21\u8bcd\u8bed\uff0c\u4f8b\u5982&quot;the&quot;\u3001&quot;is&quot;\u7b49\u3002nltk\u5e93\u63d0\u4f9b\u4e86\u53bb\u9664\u8fd9\u4e9b\u8bcd\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import nltk<\/p>\n<p>from nltk.corpus import stopwords<\/p>\n<p>nltk.download(&#39;stopwords&#39;)<\/p>\n<p>text = &quot;This is a simple example showing how to remove stopwords.&quot;<\/p>\n<p>words = text.split()<\/p>\n<p>filtered_words = [word for word in words if word.lower() not in stopwords.words(&#39;english&#39;)]<\/p>\n<p>cleaned_text = &#39; &#39;.join(filtered_words)<\/p>\n<p>print(cleaned_text)  # \u8f93\u51fa: simple example showing remove stopwords.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528nltk\u7684<code>stopwords<\/code>\u6765\u53bb\u9664\u6587\u672c\u4e2d\u7684\u505c\u7528\u8bcd\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u81ea\u7136\u8bed\u8a00\u5904\u7406\u4efb\u52a1<\/strong>\uff0c\u7279\u522b\u662f\u5728\u5206\u6790\u6587\u672c\u6570\u636e\u65f6\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528spacy\u8fdb\u884c\u5b9e\u4f53\u8bc6\u522b\u548c\u53bb\u9664<\/h4>\n<\/p>\n<p><p>spacy\u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u6587\u672c\u5904\u7406\u5e93\uff0c\u5177\u6709\u8bc6\u522b\u548c\u53bb\u9664\u7279\u5b9a\u5b9e\u4f53\u7684\u529f\u80fd\uff0c\u4f8b\u5982\u4eba\u540d\u3001\u5730\u540d\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import spacy<\/p>\n<p>nlp = spacy.load(&quot;en_core_web_sm&quot;)<\/p>\n<p>text = &quot;Barack Obama was the 44th President of the United States.&quot;<\/p>\n<p>doc = nlp(text)<\/p>\n<p>cleaned_text = &#39; &#39;.join([token.text for token in doc if not token.ent_type_])<\/p>\n<p>print(cleaned_text)  # \u8f93\u51fa: was the 44th of the<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528spacy\u8bc6\u522b\u6587\u672c\u4e2d\u7684\u5b9e\u4f53\u5e76\u53bb\u9664\u5b83\u4eec\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u8bc6\u522b\u548c\u8fc7\u6ee4\u7279\u5b9a\u7c7b\u578b\u6587\u672c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u53bb\u6389\u6587\u5b57\u7684\u65b9\u6cd5\u6709\u591a\u79cd\u9009\u62e9\uff0c<strong>\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u6548\u7387\u548c\u5904\u7406\u80fd\u529b<\/strong>\u3002\u5b57\u7b26\u4e32\u66ff\u6362\u9002\u7528\u4e8e\u7b80\u5355\u7684\u66ff\u6362\u4efb\u52a1\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u9002\u7528\u4e8e\u590d\u6742\u548c\u7075\u6d3b\u7684\u6a21\u5f0f\u5339\u914d\u4e0e\u66ff\u6362\uff0c\u800c\u6587\u672c\u5904\u7406\u5e93\u63d0\u4f9b\u4e86\u9ad8\u7ea7\u7684\u81ea\u7136\u8bed\u8a00\u5904\u7406\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u6587\u672c\u5206\u6790\u4efb\u52a1\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u5f85\u5904\u7406\u6587\u672c\u7684\u590d\u6742\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u5b9a\u5b57\u7b26\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>replace()<\/code>\u65b9\u6cd5\u6765\u5220\u9664\u7279\u5b9a\u5b57\u7b26\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u8981\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u6bcd&#39;a&#39;\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">text = &quot;Hello World&quot;\nnew_text = text.replace(&#39;a&#39;, &#39;&#39;)  # \u5c06&#39;a&#39;\u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32\nprint(new_text)  # \u8f93\u51fa: Hello World\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5220\u9664\u4efb\u610f\u5b57\u7b26\uff0c\u901a\u8fc7\u5c06\u76ee\u6807\u5b57\u7b26\u66ff\u6362\u4e3a\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32\u5373\u53ef\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u53bb\u6389\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u683c\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u63d0\u4f9b\u4e86<code>strip()<\/code>\u3001<code>lstrip()<\/code>\u548c<code>rstrip()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e8e\u53bb\u6389\u5b57\u7b26\u4e32\u5f00\u5934\u548c\u7ed3\u5c3e\u7684\u7a7a\u683c\u3002\u82e5\u60f3\u53bb\u6389\u6240\u6709\u7a7a\u683c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>replace()<\/code>\u65b9\u6cd5\u6216<code>join()<\/code>\u4e0e<code>split()<\/code>\u7ec4\u5408\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">text = &quot;  Hello World  &quot;\nnew_text = text.strip()  # \u53bb\u6389\u5f00\u5934\u548c\u7ed3\u5c3e\u7684\u7a7a\u683c\nprint(new_text)  # \u8f93\u51fa: Hello World\n\n# \u53bb\u6389\u6240\u6709\u7a7a\u683c\nno_spaces = &#39;&#39;.join(text.split())\nprint(no_spaces)  # \u8f93\u51fa: HelloWorld\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u53bb\u6389\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u6216\u7279\u5b9a\u6a21\u5f0f\uff1f<\/strong><br \/>\u53ef\u4ee5\u5229\u7528Python\u7684<code>re<\/code>\u6a21\u5757\u6765\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53bb\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u6216\u5176\u4ed6\u7279\u5b9a\u6a21\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u53bb\u6389\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u6570\u5b57\uff1a  <\/p>\n<pre><code class=\"language-python\">import re\n\ntext = &quot;Hello123 World456&quot;\nnew_text = re.sub(r&#39;\\d+&#39;, &#39;&#39;, text)  # \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u66ff\u6362\u6240\u6709\u6570\u5b57\nprint(new_text)  # \u8f93\u51fa: Hello World\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7075\u6d3b\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5236\u5b9a\u590d\u6742\u7684\u6a21\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u53bb\u6389\u6587\u5b57\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5b57\u7b26\u4e32\u66ff\u6362\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u6587\u672c\u5904\u7406\u5e93\uff08\u5982nltk\uff09\u3002\u5176\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u6700 [&hellip;]","protected":false},"author":3,"featured_media":980668,"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\/980663"}],"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=980663"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/980663\/revisions"}],"predecessor-version":[{"id":980671,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/980663\/revisions\/980671"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/980668"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=980663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=980663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=980663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}