{"id":313281,"date":"2024-05-20T23:00:30","date_gmt":"2024-05-20T15:00:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/313281.html"},"modified":"2024-05-20T23:00:43","modified_gmt":"2024-05-20T15:00:43","slug":"python%e6%80%8e%e4%b9%88%e6%8a%8a%e4%b8%8b%e9%9d%a2%e7%9a%84%e6%96%87%e5%ad%97%e6%a0%87%e7%82%b9%e7%ac%a6%e5%8f%b7%e4%b8%8e%e7%a9%ba%e6%a0%bc%ef%bc%8c%e6%9b%bf%e6%8d%a2%ef%bc%8c%e6%8d%a2%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/313281.html","title":{"rendered":"python\u600e\u4e48\u628a\u4e0b\u9762\u7684\u6587\u5b57\u6807\u70b9\u7b26\u53f7\u4e0e\u7a7a\u683c\uff0c\u66ff\u6362\uff0c\u6362\u884c"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26234032\/a61c4015-10a1-46bf-b054-802d011f4337.webp\" alt=\"python\u600e\u4e48\u628a\u4e0b\u9762\u7684\u6587\u5b57\u6807\u70b9\u7b26\u53f7\u4e0e\u7a7a\u683c\uff0c\u66ff\u6362\uff0c\u6362\u884c\" \/><\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5904\u7406\u5b57\u7b26\u4e32\uff0c\u5c24\u5176\u662f\u66ff\u6362\u6587\u672c\u4e2d\u7684\u6807\u70b9\u7b26\u53f7\u3001\u7a7a\u683c\u4ee5\u53ca\u5b9e\u73b0\u6362\u884c\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b57\u7b26\u4e32\u7684\u5185\u7f6e\u65b9\u6cd5\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u5b8c\u6210\u3002<strong>\u5148\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5904\u7406\u6807\u70b9\u7b26\u53f7\u548c\u7a7a\u683c\uff0c\u63a5\u7740\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u5b9e\u73b0\u6362\u884c<\/strong>\u3002\u9996\u5148\u8be6\u7ec6\u63cf\u8ff0\u5904\u7406\u6807\u70b9\u7b26\u53f7\u548c\u7a7a\u683c\uff1a\u53ef\u4ee5\u5229\u7528<code>re<\/code>\u6a21\u5757\u4e2d\u7684<code>sub()<\/code>\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u4e2d\u627e\u5230\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u7684\u6240\u6709\u5b50\u4e32\uff0c\u7528\u4e00\u4e2a\u4e0d\u540c\u7684\u5b57\u7b26\u4e32\u66ff\u6362\u3002\u4f8b\u5982\uff0c\u8981\u66ff\u6362\u6587\u672c\u4e2d\u7684\u6807\u70b9\u7b26\u53f7\u548c\u7a7a\u683c\uff0c\u53ef\u4ee5\u901a\u8fc7\u5efa\u7acb\u4e00\u4e2a\u5305\u542b\u6240\u6709\u6807\u70b9\u7b26\u53f7\u548c\u7a7a\u683c\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u6765\u5b9e\u73b0\u66ff\u6362\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u5f15\u5165\u5fc5\u8981\u7684\u6a21\u5757<\/h2>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6587\u672c\u66ff\u6362\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u5f15\u5165Python\u4e2d\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u7684<code>re<\/code>\u6a21\u5757\u3002<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5bf9\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u652f\u6301\uff0c\u4f7f\u5f97\u5339\u914d\u548c\u66ff\u6362\u6587\u672c\u53d8\u5f97\u5bb9\u6613\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e00\u6b65\u662f\u5fc5\u9700\u7684\uff0c\u56e0\u4e3a\u6211\u4eec\u5373\u5c06\u4f7f\u7528<code>re<\/code>\u6a21\u5757\u4e2d\u7684<code>sub()<\/code>\u65b9\u6cd5\u6765\u5904\u7406\u6587\u672c\u66ff\u6362\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u5904\u7406\u6807\u70b9\u7b26\u53f7\u548c\u7a7a\u683c<\/h2>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4e13\u6ce8\u4e8e\u5982\u4f55\u4f7f\u7528<code>re<\/code>\u6a21\u5757\u6765\u66ff\u6362\u6587\u672c\u4e2d\u7684\u6807\u70b9\u7b26\u53f7\u548c\u7a7a\u683c\u3002\u6807\u70b9\u7b26\u53f7\u5305\u62ec\u9017\u53f7\u3001\u53e5\u53f7\u3001\u5206\u53f7\u7b49\uff0c\u800c\u7a7a\u683c\u5219\u662f\u6587\u672c\u4e2d\u5e38\u89c1\u7684\u5206\u9694\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def replace_punctuation_and_spaces(text):<\/p>\n<p>    # \u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u5927\u90e8\u5206\u5e38\u89c1\u6807\u70b9\u7b26\u53f7\u548c\u7a7a\u683c\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f<\/p>\n<p>    pattern = r&#039;[,.!;:\\s]+&#039;<\/p>\n<p>    # \u4f7f\u7528\u7a7a\u5b57\u7b26\u4e32\u66ff\u6362\u627e\u5230\u7684\u6240\u6709\u6807\u70b9\u7b26\u53f7\u548c\u7a7a\u683c\uff0c\u5373\u5220\u9664\u5b83\u4eec<\/p>\n<p>    replaced_text = re.sub(pattern, &#039;&#039;, text)<\/p>\n<p>    return replaced_text<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u51fd\u6570\u5229\u7528<code>sub()<\/code>\u65b9\u6cd5\u68c0\u7d22\u6587\u672c\uff0c\u5c06\u6240\u6709\u5339\u914d\u5230\u7684\u6807\u70b9\u7b26\u53f7\u548c\u7a7a\u683c\u66ff\u6362\u6210\u7a7a\u5b57\u7b26\u4e32\uff0c\u4ece\u800c\u5b9e\u73b0\u5220\u9664\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u5b9e\u73b0\u6362\u884c\u7684\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>\u6362\u884c\u5728\u6587\u672c\u5904\u7406\u4e2d\u662f\u5f88\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u5c24\u5176\u662f\u5728\u683c\u5f0f\u5316\u8f93\u51fa\u6216\u6587\u4ef6\u5199\u5165\u65f6\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u4e2d\u6dfb\u52a0\u7279\u6b8a\u5b57\u7b26<code>\\n<\/code>\u6765\u5b9e\u73b0\u6362\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add_new_line(text):<\/p>\n<p>    # \u9009\u62e9\u4e00\u4e2a\u4f4d\u7f6e\u63d2\u5165\u6362\u884c\u7b26\\n\uff0c\u8fd9\u91cc\u4ee5\u6bcf30\u4e2a\u5b57\u7b26\u4e3a\u4f8b<\/p>\n<p>    new_line_text = &#039;\\n&#039;.join(text[i:i+30] for i in range(0, len(text), 30))<\/p>\n<p>    return new_line_text<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e2a\u51fd\u6570\uff0c\u6211\u4eec\u80fd\u591f\u5c06\u4e00\u4e2a\u957f\u5b57\u7b26\u4e32\u5206\u5272\u6210\u591a\u4e2a\u8f83\u77ed\u7684\u90e8\u5206\uff0c\u5e76\u5728\u6bcf\u90e8\u5206\u540e\u9762\u6dfb\u52a0\u6362\u884c\u7b26<code>\\n<\/code>\uff0c\u4ee5\u6b64\u6765\u5b9e\u73b0\u6362\u884c\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u6574\u5408\u5e94\u7528<\/h2>\n<\/p>\n<p><p>\u5c06\u4ee5\u4e0a\u65b9\u6cd5\u6574\u5408\u6210\u4e00\u4e2a\u5e94\u7528\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u7ed9\u5b9a\u6587\u672c\u7684\u6807\u70b9\u7b26\u53f7\u548c\u7a7a\u683c\u7684\u66ff\u6362\uff0c\u4ee5\u53ca\u6309\u9700\u6362\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def format_text(text):<\/p>\n<p>    # \u9996\u5148\uff0c\u66ff\u6362\u6587\u672c\u4e2d\u7684\u6807\u70b9\u7b26\u53f7\u548c\u7a7a\u683c<\/p>\n<p>    text_without_punctuation_and_spaces = replace_punctuation_and_spaces(text)<\/p>\n<p>    # \u63a5\u7740\uff0c\u6309\u7167\u4e00\u5b9a\u957f\u5ea6\u63d2\u5165\u6362\u884c\u7b26<\/p>\n<p>    formatted_text = add_new_line(text_without_punctuation_and_spaces)<\/p>\n<p>    return formatted_text<\/p>\n<h2><strong>\u793a\u4f8b\u6587\u672c<\/strong><\/h2>\n<p>sample_text = &quot;Hello, world! This is a test text to demonstrate, how to remove punctuation and spaces. And also, how to add new lines.&quot;<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u6587\u672c<\/strong><\/h2>\n<p>formatted_text = format_text(sample_text)<\/p>\n<p>print(formatted_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u6211\u4eec\u5df2\u7ecf\u5c55\u793a\u4e86\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u548c\u683c\u5f0f\u5316\u6587\u672c\uff0c\u5305\u62ec\u5220\u9664\u6807\u70b9\u7b26\u53f7\u548c\u7a7a\u683c\uff0c\u4ee5\u53ca\u5982\u4f55\u6dfb\u52a0\u6362\u884c\u7b26\u6765\u6539\u5584\u6587\u672c\u7684\u53ef\u8bfb\u6027\u3002\u8fd9\u79cd\u65b9\u6cd5\u65e2\u9002\u7528\u4e8e\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u5904\u7406\uff0c\u4e5f\u9002\u7528\u4e8e\u590d\u6742\u7684\u6587\u672c\u683c\u5f0f\u5316\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u672c\u6570\u636e\u65f6\uff0c\u8fd9\u4e9b\u6280\u80fd\u5c24\u5176\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u4eec\u53ef\u4ee5\u5e2e\u52a9\u6e05\u7406\u6570\u636e\u3001\u51c6\u5907\u7528\u4e8e\u8fdb\u4e00\u6b65\u5206\u6790\u7684\u6570\u636e\u683c\u5f0f\uff0c\u6216\u8005\u4ec5\u4ec5\u6539\u5584\u6587\u672c\u5185\u5bb9\u7684\u8868\u73b0\u5f62\u5f0f\u3002\u5229\u7528<code>re<\/code>\u6a21\u5757\u548c\u5b57\u7b26\u4e32\u65b9\u6cd5\uff0cPython\u7a0b\u5e8f\u5458\u80fd\u591f\u9ad8\u6548\u3001\u7075\u6d3b\u5730\u5904\u7406\u5404\u79cd\u6587\u672c\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. Python\u4e2d\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u66ff\u6362\u6587\u672c\u4e2d\u7684\u6807\u70b9\u7b26\u53f7\u548c\u7a7a\u683c\uff1f<\/strong><\/p>\n<p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528re\u6a21\u5757\u4e2d\u7684sub\u65b9\u6cd5\u6765\u66ff\u6362\u6587\u672c\u4e2d\u7684\u6807\u70b9\u7b26\u53f7\u548c\u7a7a\u683c\u3002\u9996\u5148\uff0c\u5bfc\u5165re\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528re.sub\u65b9\u6cd5\u6765\u6267\u884c\u66ff\u6362\u64cd\u4f5c\u3002\u901a\u8fc7\u6307\u5b9a\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff0c\u53ef\u4ee5\u5339\u914d\u5230\u9700\u8981\u88ab\u66ff\u6362\u7684\u6807\u70b9\u7b26\u53f7\u548c\u7a7a\u683c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528&quot;\\W+&quot;\u6765\u5339\u914d\u6807\u70b9\u7b26\u53f7\uff08\\W\u8868\u793a\u9664\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf\u5916\u7684\u5b57\u7b26\uff09\uff0c\u7528&quot;\\s+&quot;\u6765\u5339\u914d\u7a7a\u683c\uff08\\s\u8868\u793a\u7a7a\u767d\u5b57\u7b26\uff09\uff0c\u7136\u540e\u4f7f\u7528\u7a7a\u5b57\u7b26\u4e32&quot;&quot;\u6765\u66ff\u6362\u5b83\u4eec\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u5c06\u6587\u672c\u4e2d\u7684\u6807\u70b9\u7b26\u53f7\u548c\u7a7a\u683c\u66ff\u6362\u6210\u7a7a\u5b57\u7b26\u4e32\uff0c\u5b9e\u73b0\u53bb\u9664\u64cd\u4f5c\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u4f7f\u7528Python\u5c06\u6587\u672c\u4e2d\u7684\u6362\u884c\u7b26\u66ff\u6362\u6210\u7a7a\u5b57\u7b26\u4e32\uff1f<\/strong><\/p>\n<p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684replace\u65b9\u6cd5\u6765\u66ff\u6362\u6362\u884c\u7b26\u3002\u5047\u8bbe\u8981\u5c06\u6587\u672c\u4e2d\u7684\u6362\u884c\u7b26&quot;\\n&quot;\u66ff\u6362\u6210\u7a7a\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528replace\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0c\u5982\uff1atext.replace(&quot;\\n&quot;, &quot;&quot;)\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5c06\u6587\u672c\u4e2d\u7684\u6362\u884c\u7b26\u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>3. Python\u6709\u6ca1\u6709\u73b0\u6210\u7684\u5e93\u6216\u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u5c06\u6587\u672c\u4e2d\u7684\u6807\u70b9\u7b26\u53f7\u3001\u7a7a\u683c\u548c\u6362\u884c\u8fdb\u884c\u66ff\u6362\uff1f<\/strong><\/p>\n<p>\u662f\u7684\uff0cPython\u4e2d\u6709\u73b0\u6210\u7684\u5e93\u548c\u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5904\u7406\u6587\u672c\u4e2d\u7684\u6807\u70b9\u7b26\u53f7\u3001\u7a7a\u683c\u548c\u6362\u884c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528string.punctuation\u6765\u83b7\u53d6\u6240\u6709\u7684\u6807\u70b9\u7b26\u53f7\uff0c\u4f7f\u7528re\u6a21\u5757\u7684sub\u65b9\u6cd5\u7ed3\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u66ff\u6362\u6807\u70b9\u7b26\u53f7\u548c\u7a7a\u683c\uff0c\u4ee5\u53ca\u4f7f\u7528\u5b57\u7b26\u4e32\u7684replace\u65b9\u6cd5\u6765\u66ff\u6362\u6362\u884c\u7b26\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982NLTK\uff08Natural Language Toolkit\uff09\u6765\u8fdb\u884c\u66f4\u590d\u6742\u7684\u6587\u672c\u5904\u7406\u548c\u66ff\u6362\u64cd\u4f5c\u3002\u4e0d\u8fc7\uff0c\u5728\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u4e4b\u524d\uff0c\u9996\u5148\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86\u76f8\u5e94\u7684\u5e93\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5904\u7406\u5b57\u7b26\u4e32\uff0c\u5c24\u5176\u662f\u66ff\u6362\u6587\u672c\u4e2d\u7684\u6807\u70b9\u7b26\u53f7\u3001\u7a7a\u683c\u4ee5\u53ca\u5b9e\u73b0\u6362\u884c\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b57\u7b26\u4e32\u7684\u5185\u7f6e\u65b9\u6cd5\u548c\u6b63\u5219\u8868\u8fbe [&hellip;]","protected":false},"author":3,"featured_media":313301,"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\/313281"}],"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=313281"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/313281\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/313301"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=313281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=313281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=313281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}