{"id":1141347,"date":"2025-01-08T22:30:04","date_gmt":"2025-01-08T14:30:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1141347.html"},"modified":"2025-01-08T22:30:06","modified_gmt":"2025-01-08T14:30:06","slug":"python%e5%a6%82%e4%bd%95%e5%8e%bb%e9%99%a4%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b8%ad%e9%97%b4%e7%9a%84%e7%a9%ba%e7%99%bd%e7%ac%a6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1141347.html","title":{"rendered":"python\u5982\u4f55\u53bb\u9664\u5b57\u7b26\u4e32\u4e2d\u95f4\u7684\u7a7a\u767d\u7b26"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104107\/2398b29f-0137-4adf-b60f-582f69be4460.webp\" alt=\"python\u5982\u4f55\u53bb\u9664\u5b57\u7b26\u4e32\u4e2d\u95f4\u7684\u7a7a\u767d\u7b26\" \/><\/p>\n<p><p> <strong>Python\u53bb\u9664\u5b57\u7b26\u4e32\u4e2d\u95f4\u7684\u7a7a\u767d\u7b26<\/strong>\uff1a\u4f7f\u7528<code>replace<\/code>\u65b9\u6cd5\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3002\u4ee5\u4e0b\u8be6\u7ec6\u63cf\u8ff0\u4e00\u79cd\u65b9\u6cd5\uff0c\u5373\u4f7f\u7528<code>replace<\/code>\u65b9\u6cd5\u6765\u53bb\u9664\u5b57\u7b26\u4e32\u4e2d\u95f4\u7684\u7a7a\u767d\u7b26\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>replace<\/code>\u65b9\u6cd5\u662f\u6700\u7b80\u5355\u4e14\u76f4\u89c2\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7\u8c03\u7528\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684<code>replace<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u6240\u6709\u7684\u7a7a\u767d\u7b26\uff08\u5305\u62ec\u7a7a\u683c\u3001\u5236\u8868\u7b26\u7b49\uff09\u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string_with_spaces = &quot;Hello World, How Are You?&quot;<\/p>\n<p>string_without_spaces = string_with_spaces.replace(&quot; &quot;, &quot;&quot;)<\/p>\n<p>print(string_without_spaces)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001replace\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>replace<\/code>\u65b9\u6cd5\u662fPython\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684\u4e00\u4e2a\u5185\u7f6e\u65b9\u6cd5\u3002\u5b83\u5141\u8bb8\u4f60\u6307\u5b9a\u4e00\u4e2a\u5b50\u5b57\u7b26\u4e32\uff0c\u5e76\u7528\u53e6\u4e00\u4e2a\u5b50\u5b57\u7b26\u4e32\u66ff\u6362\u5b83\u3002\u8be5\u65b9\u6cd5\u7684\u8bed\u6cd5\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str.replace(old, new[, count])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>old<\/code>\uff1a\u5c06\u88ab\u66ff\u6362\u7684\u5b50\u5b57\u7b26\u4e32\u3002<\/li>\n<li><code>new<\/code>\uff1a\u7528\u4e8e\u66ff\u6362\u7684\u5b50\u5b57\u7b26\u4e32\u3002<\/li>\n<li><code>count<\/code>\uff08\u53ef\u9009\uff09\uff1a\u6307\u5b9a\u66ff\u6362\u7684\u6700\u5927\u6b21\u6570\u3002\u5982\u679c\u4e0d\u63d0\u4f9b\uff0c\u9ed8\u8ba4\u4e3a\u66ff\u6362\u6240\u6709\u51fa\u73b0\u7684\u5b50\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ul>\n<p><p>\u7528\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u5feb\u901f\u7b80\u5355\u5730\u79fb\u9664\u5b57\u7b26\u4e32\u4e2d\u95f4\u7684\u6240\u6709\u7a7a\u767d\u7b26\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\u662f\u5904\u7406\u5b57\u7b26\u4e32\u7684\u5f3a\u5927\u5de5\u5177\u3002\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u79fb\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u7a7a\u767d\u7b26\uff08\u5305\u62ec\u7a7a\u683c\u3001\u5236\u8868\u7b26\u3001\u6362\u884c\u7b26\u7b49\uff09\u3002Python\u7684<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5bf9\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>string_with_spaces = &quot;Hello World,\\tHow Are You?&quot;<\/p>\n<p>string_without_spaces = re.sub(r&#39;\\s+&#39;, &#39;&#39;, string_with_spaces)<\/p>\n<p>print(string_without_spaces)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>re.sub<\/code>\u51fd\u6570\u7528\u4e8e\u66ff\u6362\u6240\u6709\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f<code>r&#39;\\s+&#39;<\/code>\u7684\u90e8\u5206\uff0c\u5176\u4e2d<code>\\s+<\/code>\u8868\u793a\u4e00\u4e2a\u6216\u591a\u4e2a\u7a7a\u767d\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u975e\u5e38Pythonic\u7684\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u5728\u4e0d\u4f7f\u7528\u4efb\u4f55\u5916\u90e8\u5e93\u7684\u60c5\u51b5\u4e0b\uff0c\u5feb\u901f\u79fb\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u767d\u7b26\u3002\u901a\u8fc7\u904d\u5386\u5b57\u7b26\u4e32\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5e76\u4ec5\u5c06\u975e\u7a7a\u767d\u7b26\u5b57\u7b26\u4fdd\u7559\u5230\u65b0\u7684\u5b57\u7b26\u4e32\u4e2d\uff0c\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string_with_spaces = &quot;Hello World, How Are You?&quot;<\/p>\n<p>string_without_spaces = &#39;&#39;.join([char for char in string_with_spaces if not char.isspace()])<\/p>\n<p>print(string_without_spaces)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u5217\u8868\u89e3\u6790\u6765\u904d\u5386\u5b57\u7b26\u4e32\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5e76\u4f7f\u7528<code>char.isspace()<\/code>\u65b9\u6cd5\u6765\u68c0\u67e5\u5b57\u7b26\u662f\u5426\u662f\u7a7a\u767d\u7b26\u3002\u6240\u6709\u975e\u7a7a\u767d\u7b26\u5b57\u7b26\u88ab\u8fde\u63a5\u6210\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6027\u80fd\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u65b9\u6cd5\u65f6\uff0c\u6027\u80fd\u53ef\u80fd\u662f\u4e00\u4e2a\u9700\u8981\u8003\u8651\u7684\u91cd\u8981\u56e0\u7d20\u3002\u6211\u4eec\u5c06\u5bf9\u4e0a\u8ff0\u4e09\u79cd\u65b9\u6cd5\u8fdb\u884c\u6027\u80fd\u6bd4\u8f83\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u9009\u62e9\u9002\u5408\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<h2><strong>Sample string<\/strong><\/h2>\n<p>sample_string = &quot;Hello World, How Are You? &quot; * 1000<\/p>\n<h2><strong>Using replace method<\/strong><\/h2>\n<p>time_replace = timeit.timeit(lambda: sample_string.replace(&quot; &quot;, &quot;&quot;), number=1000)<\/p>\n<p>print(f&quot;Replace method time: {time_replace}&quot;)<\/p>\n<h2><strong>Using regular expression<\/strong><\/h2>\n<p>time_regex = timeit.timeit(lambda: re.sub(r&#39;\\s+&#39;, &#39;&#39;, sample_string), number=1000)<\/p>\n<p>print(f&quot;Regex method time: {time_regex}&quot;)<\/p>\n<h2><strong>Using list comprehension<\/strong><\/h2>\n<p>time_list_comp = timeit.timeit(lambda: &#39;&#39;.join([char for char in sample_string if not char.isspace()]), number=1000)<\/p>\n<p>print(f&quot;List comprehension method time: {time_list_comp}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u53ef\u4ee5\u6bd4\u8f83\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd\uff0c\u4ece\u800c\u9009\u62e9\u6700\u4f18\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<ol>\n<li><strong>\u6587\u672c\u9884\u5904\u7406<\/strong>\uff1a\u5728\u81ea\u7136\u8bed\u8a00\u5904\u7406\uff08NLP\uff09\u4efb\u52a1\u4e2d\uff0c\u5e38\u5e38\u9700\u8981\u5bf9\u6587\u672c\u8fdb\u884c\u9884\u5904\u7406\uff0c\u5305\u62ec\u53bb\u9664\u7a7a\u767d\u7b26\uff0c\u4ee5\u4fbf\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5206\u6790\u548c\u5904\u7406\u3002<\/li>\n<li><strong>\u6570\u636e\u6e05\u7406<\/strong>\uff1a\u5728\u6570\u636e\u79d1\u5b66\u9886\u57df\uff0c\u6e05\u7406\u6570\u636e\u662f\u4e00\u4e2a\u91cd\u8981\u6b65\u9aa4\u3002\u53bb\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u767d\u7b26\u53ef\u4ee5\u5e2e\u52a9\u786e\u4fdd\u6570\u636e\u7684\u4e00\u81f4\u6027\u3002<\/li>\n<li><strong>\u7528\u6237\u8f93\u5165\u5904\u7406<\/strong>\uff1a\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u53bb\u9664\u591a\u4f59\u7684\u7a7a\u767d\u7b26\uff0c\u4ee5\u786e\u4fdd\u8f93\u5165\u7684\u6709\u6548\u6027\u548c\u4e00\u81f4\u6027\u3002<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5b57\u7b26\u7f16\u7801<\/strong>\uff1a\u786e\u4fdd\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\u7f16\u7801\u4e00\u81f4\uff0c\u4ee5\u907f\u514d\u56e0\u7f16\u7801\u95ee\u9898\u5bfc\u81f4\u7684\u9519\u8bef\u3002<\/li>\n<li><strong>\u591a\u79cd\u7a7a\u767d\u7b26<\/strong>\uff1a\u6ce8\u610f\u5904\u7406\u5404\u79cd\u7c7b\u578b\u7684\u7a7a\u767d\u7b26\uff0c\u5982\u7a7a\u683c\u3001\u5236\u8868\u7b26\u3001\u6362\u884c\u7b26\u7b49\u3002<\/li>\n<li><strong>\u6027\u80fd\u4f18\u5316<\/strong>\uff1a\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u9009\u62e9\u6027\u80fd\u8f83\u4f18\u7684\u65b9\u6cd5\uff0c\u4ee5\u63d0\u9ad8\u5904\u7406\u6548\u7387\u3002<\/li>\n<\/ol>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u53bb\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u767d\u7b26\uff0c\u5305\u62ec<code>replace<\/code>\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u548c\u5217\u8868\u89e3\u6790\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u6027\u80fd\u6bd4\u8f83\u548c\u5b9e\u9645\u5e94\u7528\u573a\u666f\u7684\u5206\u6790\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u548c\u9009\u62e9\u9002\u5408\u7684\u89e3\u51b3\u65b9\u6848\u3002\u65e0\u8bba\u662f\u5728\u6587\u672c\u9884\u5904\u7406\u3001\u6570\u636e\u6e05\u7406\u8fd8\u662f\u7528\u6237\u8f93\u5165\u5904\u7406\u4e2d\uff0c\u53bb\u9664\u7a7a\u767d\u7b26\u90fd\u662f\u4e00\u4e2a\u5e38\u89c1\u4e14\u91cd\u8981\u7684\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u53bb\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u7a7a\u767d\u7b26\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>str.replace()<\/code>\u65b9\u6cd5\u6765\u53bb\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u7a7a\u767d\u7b26\u3002\u6bd4\u5982\uff0c\u8c03\u7528<code>my_string.replace(&quot; &quot;, &quot;&quot;)<\/code>\u53ef\u4ee5\u5220\u9664\u6240\u6709\u7684\u7a7a\u683c\u3002\u5982\u679c\u9700\u8981\u53bb\u9664\u6240\u6709\u7c7b\u578b\u7684\u7a7a\u767d\u7b26\uff0c\u5305\u62ec\u5236\u8868\u7b26\u548c\u6362\u884c\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5757<code>re<\/code>\uff0c\u901a\u8fc7<code>re.sub(r&#39;\\s+&#39;, &#39;&#39;, my_string)<\/code>\u6765\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u53bb\u9664\u5b57\u7b26\u4e32\u4e2d\u95f4\u7a7a\u767d\u7b26\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u6700\u4f73\u5b9e\u8df5\u662f\u5728\u5904\u7406\u5b57\u7b26\u4e32\u65f6\uff0c\u9009\u62e9\u9002\u5408\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u5982\u679c\u53ea\u60f3\u53bb\u9664\u591a\u4f59\u7684\u7a7a\u683c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>str.split()<\/code>\u548c<code>str.join()<\/code>\u7ed3\u5408\u7684\u65b9\u5f0f\uff1a<code>&#39; &#39;.join(my_string.split())<\/code>\u3002\u8fd9\u5c06\u4f1a\u5220\u9664\u591a\u4f59\u7684\u7a7a\u683c\uff0c\u5e76\u4fdd\u6301\u5355\u8bcd\u4e4b\u95f4\u7684\u4e00\u4e2a\u7a7a\u683c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u53ea\u53bb\u9664\u5b57\u7b26\u4e32\u5f00\u5934\u548c\u7ed3\u5c3e\u7684\u7a7a\u767d\u7b26\uff1f<\/strong><br \/>\u4f7f\u7528<code>str.strip()<\/code>\u65b9\u6cd5\u80fd\u591f\u8f7b\u677e\u53bb\u9664\u5b57\u7b26\u4e32\u5f00\u5934\u548c\u7ed3\u5c3e\u7684\u7a7a\u767d\u7b26\u3002\u5982\u679c\u53ea\u60f3\u53bb\u9664\u5f00\u5934\u7684\u7a7a\u767d\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528<code>str.lstrip()<\/code>\uff0c\u800c\u53bb\u9664\u7ed3\u5c3e\u7684\u7a7a\u767d\u7b26\u5219\u4f7f\u7528<code>str.rstrip()<\/code>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u4e0d\u4f1a\u5f71\u54cd\u4e2d\u95f4\u7684\u7a7a\u767d\u7b26\uff0c\u9002\u7528\u4e8e\u9700\u8981\u4fdd\u7559\u5b57\u7b26\u4e32\u5185\u90e8\u683c\u5f0f\u7684\u60c5\u51b5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53bb\u9664\u5b57\u7b26\u4e32\u4e2d\u95f4\u7684\u7a7a\u767d\u7b26\uff1a\u4f7f\u7528replace\u65b9\u6cd5\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3002\u4ee5\u4e0b\u8be6\u7ec6\u63cf\u8ff0\u4e00\u79cd\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":1141353,"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\/1141347"}],"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=1141347"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1141347\/revisions"}],"predecessor-version":[{"id":1141355,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1141347\/revisions\/1141355"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1141353"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1141347"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1141347"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1141347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}