{"id":1033822,"date":"2024-12-31T11:45:09","date_gmt":"2024-12-31T03:45:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1033822.html"},"modified":"2024-12-31T11:45:11","modified_gmt":"2024-12-31T03:45:11","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e6%96%87%e6%9c%ac%e7%89%b9%e5%ae%9a%e8%af%8d%e6%9b%bf%e6%8d%a2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1033822.html","title":{"rendered":"python\u5982\u4f55\u5c06\u6587\u672c\u7279\u5b9a\u8bcd\u66ff\u6362"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/c0c4d0e0-ce1e-481b-994a-7f60de0473d9.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5c06\u6587\u672c\u7279\u5b9a\u8bcd\u66ff\u6362\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u5c06\u6587\u672c\u4e2d\u7684\u7279\u5b9a\u8bcd\u66ff\u6362\uff0c\u5305\u62ec\u4f7f\u7528\u5b57\u7b26\u4e32\u7684replace()\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7684sub()\u65b9\u6cd5\u3001\u4ee5\u53ca\u5176\u4ed6\u9ad8\u7ea7\u6587\u672c\u5904\u7406\u5e93\uff0c\u5982nltk\u548cspaCy\u7b49\u3002<\/strong> \u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u8ba8\u8bba\u5b83\u4eec\u7684\u4f18\u7f3a\u70b9\u4ee5\u53ca\u9002\u7528\u573a\u666f\u3002\u7279\u522b\u662f\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528Python\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u9ad8\u6548\u5730\u5b8c\u6210\u6587\u672c\u66ff\u6362\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u7684replace()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>Python\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5replace()\u662f\u6700\u7b80\u5355\u548c\u76f4\u63a5\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u9002\u7528\u4e8e\u8fdb\u884c\u7b80\u5355\u7684\u8bcd\u66ff\u6362\u3002\u5b83\u7684\u8bed\u6cd5\u975e\u5e38\u76f4\u89c2\uff1a<code>str.replace(old, new[, count])<\/code>\uff0c\u5176\u4e2d<code>old<\/code>\u662f\u8981\u66ff\u6362\u7684\u5b50\u5b57\u7b26\u4e32\uff0c<code>new<\/code>\u662f\u66ff\u6362\u540e\u7684\u5b50\u5b57\u7b26\u4e32\uff0c<code>count<\/code>\u662f\u4e00\u4e2a\u53ef\u9009\u53c2\u6570\uff0c\u6307\u5b9a\u8981\u66ff\u6362\u7684\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello world! Hello everyone!&quot;<\/p>\n<p>new_text = text.replace(&quot;Hello&quot;, &quot;Hi&quot;)<\/p>\n<p>print(new_text)  # \u8f93\u51fa: &quot;Hi world! Hi everyone!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u6613\u4e8e\u4f7f\u7528\u548c\u7406\u89e3\uff0c\u9002\u5408\u5904\u7406\u7b80\u5355\u7684\u6587\u672c\u66ff\u6362\u4efb\u52a1\u3002\u7136\u800c\uff0c\u5b83\u7684\u7f3a\u70b9\u5728\u4e8e<strong>\u65e0\u6cd5\u5904\u7406\u590d\u6742\u7684\u66ff\u6362\u89c4\u5219<\/strong>\uff0c\u4f8b\u5982\u533a\u5206\u5927\u5c0f\u5199\u3001\u4ec5\u66ff\u6362\u5355\u8bcd\u800c\u4e0d\u66ff\u6362\u5355\u8bcd\u7684\u4e00\u90e8\u5206\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7684sub()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u66ff\u6362\u9700\u6c42\uff0cPython\u7684re\u6a21\u5757\u63d0\u4f9b\u4e86sub()\u65b9\u6cd5\uff0c\u8fd9\u662f\u4e00\u79cd\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u5de5\u5177\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u5141\u8bb8\u6211\u4eec\u6307\u5b9a\u590d\u6742\u7684\u5339\u914d\u6a21\u5f0f\uff0c\u4ece\u800c\u5b9e\u73b0\u66f4\u7cbe\u7ec6\u7684\u66ff\u6362\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;Hello world! Hello everyone!&quot;<\/p>\n<p>new_text = re.sub(r&#39;\\bHello\\b&#39;, &#39;Hi&#39;, text)<\/p>\n<p>print(new_text)  # \u8f93\u51fa: &quot;Hi world! Hi everyone!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>\\b<\/code>\u6765\u6307\u5b9a\u5355\u8bcd\u8fb9\u754c\uff0c\u4ee5\u786e\u4fdd\u4ec5\u66ff\u6362\u6574\u4e2a\u5355\u8bcd\u201cHello\u201d\u800c\u4e0d\u662f\u5176\u4ed6\u5305\u542b\u201cHello\u201d\u7684\u5355\u8bcd\u3002<strong>\u6b63\u5219\u8868\u8fbe\u5f0f\u975e\u5e38\u5f3a\u5927\uff0c\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u6a21\u5f0f\u5339\u914d\u7684\u590d\u6742\u66ff\u6362\u4efb\u52a1<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5904\u7406\u5927\u5c0f\u5199\u654f\u611f\u7684\u66ff\u6362<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u9700\u8981\u8fdb\u884c\u5927\u5c0f\u5199\u654f\u611f\u7684\u66ff\u6362\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528re\u6a21\u5757\u4e2d\u7684<code>re.IGNORECASE<\/code>\u6807\u5fd7\u6765\u5ffd\u7565\u5927\u5c0f\u5199\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;Hello world! hello everyone!&quot;<\/p>\n<p>new_text = re.sub(r&#39;hello&#39;, &#39;Hi&#39;, text, flags=re.IGNORECASE)<\/p>\n<p>print(new_text)  # \u8f93\u51fa: &quot;Hi world! Hi everyone!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u80fd\u591f\u7075\u6d3b\u5730\u5904\u7406\u5927\u5c0f\u5199\u654f\u611f\u7684\u95ee\u9898\uff0c\u9002\u7528\u4e8e\u9700\u8981\u7edf\u4e00\u66ff\u6362\u5927\u5c0f\u5199\u4e0d\u540c\u5f62\u5f0f\u7684\u5355\u8bcd\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528nltk\u8fdb\u884c\u66f4\u9ad8\u7ea7\u7684\u6587\u672c\u66ff\u6362<\/h3>\n<\/p>\n<p><p>Natural Language Toolkit\uff08nltk\uff09\u662f\u4e00\u4e2a\u5f3a\u5927\u7684Python\u5e93\uff0c\u4e13\u95e8\u7528\u4e8e\u5904\u7406\u81ea\u7136\u8bed\u8a00\u6587\u672c\u3002\u901a\u8fc7nltk\uff0c\u6211\u4eec\u53ef\u4ee5\u8fdb\u884c\u66f4\u9ad8\u7ea7\u7684\u6587\u672c\u5904\u7406\uff0c\u4f8b\u5982\u8bcd\u6027\u6807\u6ce8\u3001\u8bcd\u5e72\u63d0\u53d6\u7b49\uff0c\u4ece\u800c\u5b9e\u73b0\u66f4\u52a0\u667a\u80fd\u7684\u66ff\u6362\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import nltk<\/p>\n<p>from nltk.tokenize import word_tokenize<\/p>\n<p>text = &quot;Hello world! Hello everyone!&quot;<\/p>\n<p>tokens = word_tokenize(text)<\/p>\n<p>new_tokens = [&quot;Hi&quot; if token == &quot;Hello&quot; else token for token in tokens]<\/p>\n<p>new_text = &#39; &#39;.join(new_tokens)<\/p>\n<p>print(new_text)  # \u8f93\u51fa: &quot;Hi world! Hi everyone!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u7ed3\u5408\u81ea\u7136\u8bed\u8a00\u5904\u7406\u6280\u672f\u8fdb\u884c\u590d\u6742\u6587\u672c\u66ff\u6362\u7684\u573a\u666f<\/strong>\uff0c\u4f46\u9700\u8981\u5b89\u88c5\u548c\u914d\u7f6enltk\u5e93\uff0c\u5e76\u4e14\u4ee3\u7801\u76f8\u5bf9\u590d\u6742\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528spaCy\u8fdb\u884c\u6587\u672c\u66ff\u6362<\/h3>\n<\/p>\n<p><p>spaCy\u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u5305\u62ec\u8bcd\u6027\u6807\u6ce8\u3001\u547d\u540d\u5b9e\u4f53\u8bc6\u522b\u7b49\u3002\u901a\u8fc7spaCy\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u667a\u80fd\u5730\u8fdb\u884c\u6587\u672c\u66ff\u6362\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\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;Hello world! Hello everyone!&quot;<\/p>\n<p>doc = nlp(text)<\/p>\n<p>new_tokens = [&quot;Hi&quot; if token.text == &quot;Hello&quot; else token.text for token in doc]<\/p>\n<p>new_text = &#39; &#39;.join(new_tokens)<\/p>\n<p>print(new_text)  # \u8f93\u51fa: &quot;Hi world! Hi everyone!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f7f\u7528spaCy\u53ef\u4ee5\u7ed3\u5408\u5176\u5f3a\u5927\u7684\u81ea\u7136\u8bed\u8a00\u5904\u7406\u80fd\u529b\u6765\u8fdb\u884c\u66f4\u7cbe\u7ec6\u7684\u6587\u672c\u66ff\u6362<\/strong>\uff0c\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u7ea7\u6587\u672c\u5206\u6790\u548c\u5904\u7406\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u9762\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u6587\u672c\u7279\u5b9a\u8bcd\u7684\u66ff\u6362\uff0c\u4ece\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\u5230\u5f3a\u5927\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u4ee5\u53ca\u9ad8\u7ea7\u7684\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5e93\u5982nltk\u548cspaCy\u3002<strong>\u5728\u9009\u62e9\u5177\u4f53\u7684\u65b9\u6cd5\u65f6\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u6765\u51b3\u5b9a<\/strong>\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u66ff\u6362\u4efb\u52a1\uff0c\u5b57\u7b26\u4e32\u7684replace()\u65b9\u6cd5\u5df2\u7ecf\u8db3\u591f\uff1b\u800c\u5bf9\u4e8e\u590d\u6742\u7684\u66ff\u6362\u4efb\u52a1\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u548c\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5e93\u5219\u80fd\u591f\u63d0\u4f9b\u66f4\u5f3a\u5927\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u638c\u63e1\u8fd9\u4e9b\u6280\u672f\u90fd\u5c06\u5927\u5927\u63d0\u9ad8\u6211\u4eec\u5904\u7406\u6587\u672c\u6570\u636e\u7684\u80fd\u529b\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u8981\u7075\u6d3b\u7ec4\u5408\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4ee5\u5b9e\u73b0\u6700\u4f73\u7684\u66ff\u6362\u6548\u679c\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u6280\u672f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u66ff\u6362\u6587\u672c\u4e2d\u7684\u7279\u5b9a\u8bcd\u6c47\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>replace()<\/code>\u65b9\u6cd5\u6765\u66ff\u6362\u6587\u672c\u4e2d\u7684\u7279\u5b9a\u8bcd\u6c47\u3002\u4f8b\u5982\uff0c<code>text.replace(&quot;\u65e7\u8bcd&quot;, &quot;\u65b0\u8bcd&quot;)<\/code>\u5c06\u4f1a\u628a\u6587\u672c\u4e2d\u7684\u6240\u6709\u201c\u65e7\u8bcd\u201d\u66ff\u6362\u6210\u201c\u65b0\u8bcd\u201d\u3002\u53e6\u5916\uff0c\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u66ff\u6362\u9700\u6c42\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>re<\/code>\u6a21\u5757\u4e2d\u7684<code>sub()<\/code>\u51fd\u6570\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u5927\u5c0f\u5199\u4e0d\u654f\u611f\u7684\u8bcd\u6c47\u66ff\u6362\uff1f<\/strong><br \/>\u4f7f\u7528<code>re<\/code>\u6a21\u5757\u7684<code>sub()<\/code>\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u5927\u5c0f\u5199\u4e0d\u654f\u611f\u7684\u66ff\u6362\u3002\u901a\u8fc7\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528<code>re.IGNORECASE<\/code>\u6807\u5fd7\uff0c\u53ef\u4ee5\u627e\u5230\u5e76\u66ff\u6362\u6587\u672c\u4e2d\u6240\u6709\u5f62\u5f0f\u7684\u7279\u5b9a\u8bcd\u6c47\u3002\u4f8b\u5982\uff0c<code>re.sub(&quot;\u65e7\u8bcd&quot;, &quot;\u65b0\u8bcd&quot;, text, flags=re.IGNORECASE)<\/code>\u4f1a\u66ff\u6362\u6587\u672c\u4e2d\u6240\u6709\u201c\u65e7\u8bcd\u201d\u7684\u5f62\u5f0f\uff0c\u5305\u62ec\u201c\u65e7\u8bcd\u201d\u3001\u201c\u65e7\u8bcd\u201d\u3001\u201c\u65e7\u8bcd\u201d\u7b49\u3002<\/p>\n<p><strong>\u80fd\u5426\u4f7f\u7528Python\u540c\u65f6\u66ff\u6362\u591a\u4e2a\u7279\u5b9a\u8bcd\u6c47\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u6216\u5b57\u5178\u7684\u65b9\u5f0f\u6765\u540c\u65f6\u66ff\u6362\u591a\u4e2a\u7279\u5b9a\u8bcd\u6c47\u3002\u4e00\u4e2a\u7b80\u5355\u7684\u5b9e\u73b0\u65b9\u6cd5\u662f\u4f7f\u7528\u5b57\u5178\u5b58\u50a8\u9700\u8981\u66ff\u6362\u7684\u8bcd\u6c47\u53ca\u5176\u5bf9\u5e94\u7684\u65b0\u8bcd\uff0c\u7136\u540e\u9010\u4e2a\u8fdb\u884c\u66ff\u6362\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5faa\u73af\u904d\u5386\u5b57\u5178\uff0c\u8c03\u7528<code>text.replace(old_word, new_word)<\/code>\u65b9\u6cd5\u8fdb\u884c\u66ff\u6362\u3002\u8fd9\u6837\u53ef\u4ee5\u6709\u6548\u7ba1\u7406\u591a\u4e2a\u8bcd\u6c47\u7684\u66ff\u6362\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u5c06\u6587\u672c\u4e2d\u7684\u7279\u5b9a\u8bcd\u66ff\u6362\uff0c\u5305\u62ec\u4f7f\u7528\u5b57\u7b26\u4e32\u7684replace()\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7684sub [&hellip;]","protected":false},"author":3,"featured_media":1033836,"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\/1033822"}],"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=1033822"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1033822\/revisions"}],"predecessor-version":[{"id":1033839,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1033822\/revisions\/1033839"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1033836"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1033822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1033822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1033822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}