{"id":1115776,"date":"2025-01-08T18:09:58","date_gmt":"2025-01-08T10:09:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1115776.html"},"modified":"2025-01-08T18:10:00","modified_gmt":"2025-01-08T10:10:00","slug":"python%e5%a6%82%e4%bd%95replace%e5%a4%9a%e4%b8%aa%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1115776.html","title":{"rendered":"python\u5982\u4f55replace\u591a\u4e2a\u5b57\u7b26\u4e32"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25080400\/1697f0c4-fb20-48d0-b6e3-f54466dab918.webp\" alt=\"python\u5982\u4f55replace\u591a\u4e2a\u5b57\u7b26\u4e32\" \/><\/p>\n<p><p> Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\u66ff\u6362\u591a\u4e2a\u5b57\u7b26\u4e32\u3002<strong>\u4f7f\u7528replace()\u65b9\u6cd5\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u901a\u8fc7\u5b57\u5178\u6620\u5c04\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u7684translate()\u65b9\u6cd5<\/strong>\u7b49\u3002\u5176\u4e2d\uff0c\u901a\u8fc7\u5b57\u5178\u6620\u5c04\u662f\u4e00\u79cd\u5e38\u89c1\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u8981\u66ff\u6362\u591a\u4e2a\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u5c06\u5f85\u66ff\u6362\u7684\u5b57\u7b26\u4e32\u548c\u5bf9\u5e94\u7684\u65b0\u5b57\u7b26\u4e32\u5b58\u50a8\u5728\u5b57\u5178\u4e2d\uff0c\u7136\u540e\u904d\u5386\u5b57\u5178\u5e76\u4f7f\u7528\u5b57\u7b26\u4e32\u7684replace()\u65b9\u6cd5\u8fdb\u884c\u66ff\u6362\u3002\u8fd9\u6837\u53ef\u4ee5\u6e05\u6670\u5730\u7ba1\u7406\u548c\u66ff\u6362\u591a\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def replace_multiple_strings(text, replacements):<\/p>\n<p>    for old, new in replacements.items():<\/p>\n<p>        text = text.replace(old, new)<\/p>\n<p>    return text<\/p>\n<p>text = &quot;Hello, world! Welcome to the Python world.&quot;<\/p>\n<p>replacements = {<\/p>\n<p>    &quot;Hello&quot;: &quot;Hi&quot;,<\/p>\n<p>    &quot;world&quot;: &quot;universe&quot;,<\/p>\n<p>    &quot;Python&quot;: &quot;programming&quot;<\/p>\n<p>}<\/p>\n<p>new_text = replace_multiple_strings(text, replacements)<\/p>\n<p>print(new_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>replace_multiple_strings<\/code>\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u5b57\u7b26\u4e32\u548c\u4e00\u4e2a\u5305\u542b\u66ff\u6362\u5bf9\u7684\u5b57\u5178\u3002\u901a\u8fc7\u904d\u5386\u5b57\u5178\u4e2d\u7684\u6bcf\u4e00\u5bf9\u952e\u503c\u5bf9\uff0c\u5229\u7528<code>replace()<\/code>\u65b9\u6cd5\u9010\u4e2a\u66ff\u6362\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u4e5f\u662f\u66ff\u6362\u591a\u4e2a\u5b57\u7b26\u4e32\u7684\u6709\u6548\u65b9\u6cd5\u3002\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u4e00\u6b21\u6027\u5339\u914d\u591a\u4e2a\u6a21\u5f0f\u5e76\u8fdb\u884c\u66ff\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def replace_multiple_strings_regex(text, replacements):<\/p>\n<p>    pattern = re.compile(&quot;|&quot;.join(re.escape(key) for key in replacements.keys()))<\/p>\n<p>    return pattern.sub(lambda match: replacements[match.group(0)], text)<\/p>\n<p>text = &quot;Hello, world! Welcome to the Python world.&quot;<\/p>\n<p>replacements = {<\/p>\n<p>    &quot;Hello&quot;: &quot;Hi&quot;,<\/p>\n<p>    &quot;world&quot;: &quot;universe&quot;,<\/p>\n<p>    &quot;Python&quot;: &quot;programming&quot;<\/p>\n<p>}<\/p>\n<p>new_text = replace_multiple_strings_regex(text, replacements)<\/p>\n<p>print(new_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>re.compile()<\/code>\u6765\u7f16\u8bd1\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff0c\u8be5\u6a21\u5f0f\u5339\u914d\u5b57\u5178\u4e2d\u7684\u6240\u6709\u952e\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>pattern.sub()<\/code>\u65b9\u6cd5\u8fdb\u884c\u66ff\u6362\uff0c\u5339\u914d\u7684\u5b57\u7b26\u4e32\u5c06\u88ab\u66ff\u6362\u4e3a\u5b57\u5178\u4e2d\u7684\u76f8\u5e94\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u7684translate()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>translate()\u65b9\u6cd5\u7ed3\u5408str.maketrans()\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u591a\u5b57\u7b26\u4e32\u66ff\u6362\uff0c\u4f46\u8fd9\u79cd\u65b9\u6cd5\u901a\u5e38\u7528\u4e8e\u5355\u5b57\u7b26\u66ff\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def replace_multiple_characters(text, replacements):<\/p>\n<p>    translation_table = str.maketrans(replacements)<\/p>\n<p>    return text.translate(translation_table)<\/p>\n<p>text = &quot;Hello, world! Welcome to the Python world.&quot;<\/p>\n<p>replacements = {<\/p>\n<p>    &quot;H&quot;: &quot;h&quot;,<\/p>\n<p>    &quot;w&quot;: &quot;W&quot;,<\/p>\n<p>    &quot;P&quot;: &quot;p&quot;<\/p>\n<p>}<\/p>\n<p>new_text = replace_multiple_characters(text, replacements)<\/p>\n<p>print(new_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>str.maketrans()<\/code>\u521b\u5efa\u4e00\u4e2a\u7ffb\u8bd1\u8868\uff0c\u7136\u540e\u4f7f\u7528<code>translate()<\/code>\u65b9\u6cd5\u8fdb\u884c\u66ff\u6362\u3002\u7531\u4e8e<code>translate()<\/code>\u65b9\u6cd5\u662f\u9488\u5bf9\u5355\u5b57\u7b26\u7684\u66ff\u6362\uff0c\u56e0\u6b64\u9002\u7528\u4e8e\u8f83\u7b80\u5355\u7684\u66ff\u6362\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u901a\u8fc7\u81ea\u5b9a\u4e49\u51fd\u6570\u5b9e\u73b0\u591a\u5b57\u7b26\u4e32\u66ff\u6362<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u66f4\u901a\u7528\u7684\u51fd\u6570\u6765\u5b9e\u73b0\u591a\u5b57\u7b26\u4e32\u66ff\u6362\uff0c\u8fd9\u6837\u53ef\u4ee5\u5728\u66ff\u6362\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u66f4\u591a\u7684\u63a7\u5236\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def custom_replace(text, replacements):<\/p>\n<p>    for old, new in replacements.items():<\/p>\n<p>        text = text.replace(old, new)<\/p>\n<p>    return text<\/p>\n<p>text = &quot;Hello, world! Welcome to the Python world.&quot;<\/p>\n<p>replacements = {<\/p>\n<p>    &quot;Hello&quot;: &quot;Hi&quot;,<\/p>\n<p>    &quot;world&quot;: &quot;universe&quot;,<\/p>\n<p>    &quot;Python&quot;: &quot;programming&quot;,<\/p>\n<p>    &quot;Welcome&quot;: &quot;Greetings&quot;<\/p>\n<p>}<\/p>\n<p>new_text = custom_replace(text, replacements)<\/p>\n<p>print(new_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>custom_replace<\/code>\u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u5b57\u7b26\u4e32\u548c\u4e00\u4e2a\u5305\u542b\u66ff\u6362\u5bf9\u7684\u5b57\u5178\u3002\u901a\u8fc7\u904d\u5386\u5b57\u5178\u4e2d\u7684\u6bcf\u4e00\u5bf9\u952e\u503c\u5bf9\uff0c\u5229\u7528<code>replace()<\/code>\u65b9\u6cd5\u9010\u4e2a\u66ff\u6362\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u66ff\u6362\u9700\u6c42\uff0c\u6211\u4eec\u53ef\u4ee5\u501f\u52a9\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982<code>multistr<\/code>\uff0c\u8be5\u5e93\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u548c\u7075\u6d3b\u7684\u591a\u5b57\u7b26\u4e32\u66ff\u6362\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from multistr import multistr<\/p>\n<p>text = &quot;Hello, world! Welcome to the Python world.&quot;<\/p>\n<p>replacements = {<\/p>\n<p>    &quot;Hello&quot;: &quot;Hi&quot;,<\/p>\n<p>    &quot;world&quot;: &quot;universe&quot;,<\/p>\n<p>    &quot;Python&quot;: &quot;programming&quot;,<\/p>\n<p>    &quot;Welcome&quot;: &quot;Greetings&quot;<\/p>\n<p>}<\/p>\n<p>new_text = multistr(text).replace(replacements)<\/p>\n<p>print(new_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>multistr<\/code>\u5e93\u7684<code>replace<\/code>\u65b9\u6cd5\u8fdb\u884c\u591a\u5b57\u7b26\u4e32\u66ff\u6362\u3002\u8be5\u5e93\u63d0\u4f9b\u4e86\u66f4\u7b80\u6d01\u548c\u9ad8\u6548\u7684\u66ff\u6362\u529f\u80fd\uff0c\u975e\u5e38\u9002\u5408\u590d\u6742\u7684\u66ff\u6362\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u66ff\u6362\u591a\u4e2a\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec<strong>\u4f7f\u7528replace()\u65b9\u6cd5\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u901a\u8fc7\u5b57\u5178\u6620\u5c04\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u7684translate()\u65b9\u6cd5<\/strong>\u7b49\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u5e94\u7528\u573a\u666f\u3002\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\u548c\u793a\u4f8b\u4ee3\u7801\uff0c\u76f8\u4fe1\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86\u591a\u79cd\u66ff\u6362\u591a\u4e2a\u5b57\u7b26\u4e32\u7684\u6280\u5de7\u548c\u65b9\u6cd5\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u540c\u65f6\u66ff\u6362\u591a\u4e2a\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>str.replace()<\/code>\u65b9\u6cd5\u9010\u4e2a\u66ff\u6362\u5b57\u7b26\u4e32\uff0c\u4f46\u5f53\u9700\u8981\u66ff\u6362\u591a\u4e2a\u5b57\u7b26\u4e32\u65f6\uff0c\u4f7f\u7528\u5b57\u5178\u7ed3\u5408<code>str.translate()<\/code>\u548c<code>str.maketrans()<\/code>\u65b9\u6cd5\u4f1a\u66f4\u9ad8\u6548\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u66ff\u6362\u6620\u5c04\u5b57\u5178\uff0c\u7136\u540e\u4f7f\u7528\u8fd9\u4e24\u4e2a\u51fd\u6570\u6765\u66ff\u6362\u591a\u4e2a\u5b57\u7b26\u4e32\uff0c\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">replacements = str.maketrans({&quot;old1&quot;: &quot;new1&quot;, &quot;old2&quot;: &quot;new2&quot;})\nresult = original_string.translate(replacements)\n<\/code><\/pre>\n<p><strong>\u6709\u6ca1\u6709\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u66ff\u6362\u591a\u4e2a\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\uff1f<\/strong><br \/>\u4f7f\u7528Python\u7684<code>re<\/code>\u6a21\u5757\u53ef\u4ee5\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u5b9e\u73b0\u591a\u4e2a\u5b57\u7b26\u4e32\u7684\u66ff\u6362\u3002\u53ef\u4ee5\u5229\u7528<code>re.sub()<\/code>\u51fd\u6570\u914d\u5408\u4e00\u4e2a\u6a21\u5f0f\u5b57\u7b26\u4e32\u548c\u66ff\u6362\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u9700\u8981\u66ff\u6362\u7684\u5b57\u7b26\u4e32\u653e\u5165\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u6784\u5efa\u6b63\u5219\u8868\u8fbe\u5f0f\u5e76\u66ff\u6362\uff1a<\/p>\n<pre><code class=\"language-python\">import re\n\ndef replace_multiple(original_string):\n    replacements = {&quot;old1&quot;: &quot;new1&quot;, &quot;old2&quot;: &quot;new2&quot;}\n    pattern = re.compile(&quot;|&quot;.join(re.escape(key) for key in replacements.keys()))\n    \n    return pattern.sub(lambda match: replacements[match.group(0)], original_string)\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u5927\u5c0f\u5199\u654f\u611f\u7684\u5b57\u7b26\u4e32\u66ff\u6362\uff1f<\/strong><br \/>\u5904\u7406\u5927\u5c0f\u5199\u654f\u611f\u7684\u5b57\u7b26\u4e32\u66ff\u6362\u53ef\u4ee5\u4f7f\u7528<code>re<\/code>\u6a21\u5757\u7684<code>re.IGNORECASE<\/code>\u6807\u5fd7\u3002\u5728\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u65f6\uff0c\u53ef\u4ee5\u6307\u5b9a\u8be5\u6807\u5fd7\u6765\u5b9e\u73b0\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u66ff\u6362\u3002\u8fd9\u5bf9\u4e8e\u7528\u6237\u5728\u5904\u7406\u6587\u672c\u65f6\u975e\u5e38\u6709\u7528\uff0c\u786e\u4fdd\u66ff\u6362\u80fd\u8986\u76d6\u6240\u6709\u60c5\u51b5\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import re\n\ndef replace_case_insensitive(original_string):\n    replacements = {&quot;old&quot;: &quot;new&quot;}\n    pattern = re.compile(re.escape(&quot;old&quot;), re.IGNORECASE)\n    \n    return pattern.sub(replacements[&quot;old&quot;], original_string)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u5728Python\u4e2d\u5b9e\u73b0\u591a\u4e2a\u5b57\u7b26\u4e32\u7684\u66ff\u6362\uff0c\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\u66ff\u6362\u591a\u4e2a\u5b57\u7b26\u4e32\u3002\u4f7f\u7528replace()\u65b9\u6cd5\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u901a\u8fc7\u5b57\u5178\u6620\u5c04\u3001\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1115780,"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\/1115776"}],"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=1115776"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1115776\/revisions"}],"predecessor-version":[{"id":1115781,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1115776\/revisions\/1115781"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1115780"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1115776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1115776"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1115776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}