{"id":994365,"date":"2024-12-27T08:54:46","date_gmt":"2024-12-27T00:54:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/994365.html"},"modified":"2024-12-27T08:54:50","modified_gmt":"2024-12-27T00:54:50","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%bf%87%e6%bb%a4%e4%b8%ad%e6%96%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/994365.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8fc7\u6ee4\u4e2d\u6587"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071402\/af7a7bc9-b596-4c16-b45f-17c78a408772.webp\" alt=\"python\u4e2d\u5982\u4f55\u8fc7\u6ee4\u4e2d\u6587\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u8fdb\u884c\u4e2d\u6587\u8fc7\u6ee4\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5229\u7528\u5b57\u7b26\u4e32\u5e93\u3001\u4ee5\u53ca\u501f\u52a9\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5e93\u7b49\u3002<strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u501f\u52a9\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u4ee5\u53ca\u5229\u7528\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5e93<\/strong>\u662f\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u5176\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u6700\u5e38\u89c1\u4e14\u9ad8\u6548\u7684\u65b9\u5f0f\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\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\uff0c\u80fd\u591f\u9ad8\u6548\u5730\u5339\u914d\u548c\u64cd\u4f5c\u6587\u672c\u6570\u636e\u3002Python\u4e2d\u7684<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5168\u9762\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u652f\u6301\u3002<\/p>\n<\/p>\n<p><h4>1. \u5339\u914d\u4e2d\u6587\u5b57\u7b26<\/h4>\n<\/p>\n<p><p>\u8981\u8fc7\u6ee4\u6389\u4e2d\u6587\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u4e2d\u6587\u5b57\u7b26\u7684\u8303\u56f4\u3002\u6c49\u5b57\u5728Unicode\u4e2d\u7684\u8303\u56f4\u4e3a<code>[\\u4e00-\\u9fff]<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def remove_chinese(text):<\/p>\n<p>    return re.sub(r&#39;[\\u4e00-\\u9fff]+&#39;, &#39;&#39;, text)<\/p>\n<p>example_text = &quot;\u8fd9\u662f\u4e00\u4e2a\u4f8b\u5b50123abc&quot;<\/p>\n<p>filtered_text = remove_chinese(example_text)<\/p>\n<p>print(filtered_text)  # \u8f93\u51fa: 123abc<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u53ea\u4fdd\u7559\u4e2d\u6587\u5b57\u7b26<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u53ea\u4fdd\u7559\u4e2d\u6587\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u4f3c\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def keep_chinese(text):<\/p>\n<p>    return &#39;&#39;.join(re.findall(r&#39;[\\u4e00-\\u9fff]+&#39;, text))<\/p>\n<p>example_text = &quot;\u8fd9\u662f\u4e00\u4e2a\u4f8b\u5b50123abc&quot;<\/p>\n<p>chinese_text = keep_chinese(example_text)<\/p>\n<p>print(chinese_text)  # \u8f93\u51fa: \u8fd9\u662f\u4e00\u4e2a\u4f8b\u5b50<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5229\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u4f46\u5728\u67d0\u4e9b\u7b80\u5355\u573a\u666f\u4e0b\uff0c\u53ef\u4ee5\u5229\u7528Python\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\u8fdb\u884c\u57fa\u672c\u7684\u8fc7\u6ee4\u3002<\/p>\n<\/p>\n<p><h4>1. \u901a\u8fc7\u5b57\u7b26\u7f16\u7801\u5224\u65ad<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u7684Unicode\u7f16\u7801\u6765\u5224\u65ad\u5b57\u7b26\u662f\u5426\u4e3a\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_chinese(char):<\/p>\n<p>    return &#39;\\u4e00&#39; &lt;= char &lt;= &#39;\\u9fff&#39;<\/p>\n<p>def filter_chinese(text):<\/p>\n<p>    return &#39;&#39;.join(char for char in text if not is_chinese(char))<\/p>\n<p>example_text = &quot;\u8fd9\u662f\u4e00\u4e2a\u4f8b\u5b50123abc&quot;<\/p>\n<p>filtered_text = filter_chinese(example_text)<\/p>\n<p>print(filtered_text)  # \u8f93\u51fa: 123abc<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u76f4\u63a5\uff0c\u4f46\u5bf9\u6548\u7387\u8981\u6c42\u8f83\u9ad8\u7684\u573a\u5408\u53ef\u80fd\u4e0d\u5982\u6b63\u5219\u8868\u8fbe\u5f0f\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5229\u7528\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5e93<\/h3>\n<\/p>\n<p><p>\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5e93\u5982jieba\u3001spaCy\u7b49\u53ef\u4ee5\u63d0\u4f9b\u66f4\u9ad8\u5c42\u6b21\u7684\u6587\u672c\u5904\u7406\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528jieba\u8fdb\u884c\u4e2d\u6587\u5206\u8bcd<\/h4>\n<\/p>\n<p><p>\u867d\u7136jieba\u4e3b\u8981\u7528\u4e8e\u4e2d\u6587\u5206\u8bcd\uff0c\u4f46\u5728\u7279\u5b9a\u573a\u5408\u4e0b\u53ef\u4ee5\u7528\u4e8e\u8fc7\u6ee4\u4e2d\u6587\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import jieba<\/p>\n<p>def filter_non_chinese(text):<\/p>\n<p>    words = jieba.cut(text)<\/p>\n<p>    return &#39;&#39;.join(word for word in words if all(&#39;\\u4e00&#39; &lt;= char &lt;= &#39;\\u9fff&#39; for char in word))<\/p>\n<p>example_text = &quot;\u8fd9\u662f\u4e00\u4e2a\u4f8b\u5b50123abc&quot;<\/p>\n<p>filtered_text = filter_non_chinese(example_text)<\/p>\n<p>print(filtered_text)  # \u8f93\u51fa: \u8fd9\u662f\u4e00\u4e2a\u4f8b\u5b50<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u4ee5\u6ee1\u8db3\u7279\u5b9a\u7684\u4e1a\u52a1\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5148\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u521d\u6b65\u8fc7\u6ee4\uff0c\u7136\u540e\u4f7f\u7528\u5b57\u7b26\u7f16\u7801\u6216\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5e93\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>1. \u7ec4\u5408\u65b9\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def advanced_filter(text):<\/p>\n<p>    # \u521d\u6b65\u8fc7\u6ee4\u975e\u4e2d\u6587\u5b57\u7b26<\/p>\n<p>    initial_filtered = re.sub(r&#39;[^\\u4e00-\\u9fff]&#39;, &#39;&#39;, text)<\/p>\n<p>    # \u8fdb\u4e00\u6b65\u5904\u7406\uff0c\u5047\u8bbe\u9700\u8981\u5904\u7406\u67d0\u4e9b\u7279\u5b9a\u5b57\u7b26<\/p>\n<p>    final_filtered = &#39;&#39;.join(char for char in initial_filtered if char not in {&#39;\u7279\u5b9a\u5b57\u7b261&#39;, &#39;\u7279\u5b9a\u5b57\u7b262&#39;})<\/p>\n<p>    return final_filtered<\/p>\n<p>example_text = &quot;\u8fd9\u662f\u4e00\u4e2a\u4f8b\u5b50123abc\u7279\u5b9a\u5b57\u7b261&quot;<\/p>\n<p>filtered_text = advanced_filter(example_text)<\/p>\n<p>print(filtered_text)  # \u8f93\u51fa: \u8fd9\u662f\u4e00\u4e2a\u4f8b\u5b50<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5b9e\u7528\u573a\u666f\u4e0e\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><h4>1. \u5b9e\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p>\u8fc7\u6ee4\u4e2d\u6587\u7684\u9700\u6c42\u5728\u6570\u636e\u6e05\u6d17\u3001\u6587\u672c\u9884\u5904\u7406\u4ee5\u53ca\u6570\u636e\u5206\u6790\u4e2d\u975e\u5e38\u5e38\u89c1\u3002\u4f8b\u5982\uff0c\u5904\u7406\u7528\u6237\u751f\u6210\u5185\u5bb9\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u53bb\u9664\u975e\u4e2d\u6587\u5b57\u7b26\u4ee5\u4fbf\u8fdb\u884c\u540e\u7eed\u7684\u6587\u672c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><h4>2. \u6ce8\u610f\u4e8b\u9879<\/h4>\n<\/p>\n<ul>\n<li><strong>\u5b57\u7b26\u7f16\u7801<\/strong>\uff1a\u786e\u4fdd\u8f93\u5165\u6587\u672c\u7684\u7f16\u7801\u662f\u6b63\u786e\u7684\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u4e0d\u540c\u8bed\u8a00\u7684\u6587\u672c\u65f6\u3002<\/li>\n<li><strong>\u6027\u80fd\u8003\u8651<\/strong>\uff1a\u5bf9\u4e8e\u5927\u89c4\u6a21\u6587\u672c\u5904\u7406\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6027\u80fd\u901a\u5e38\u4f18\u4e8e\u5b57\u7b26\u9010\u4e2a\u5224\u65ad\u3002<\/li>\n<li><strong>\u591a\u8bed\u8a00\u652f\u6301<\/strong>\uff1a\u5982\u679c\u9700\u8981\u5904\u7406\u591a\u8bed\u8a00\u6587\u672c\uff0c\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u5176\u4ed6\u8bed\u8a00\u7684\u5904\u7406\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5b57\u7b26\u4e32\u65b9\u6cd5\u4ee5\u53ca\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5e93\uff0c\u80fd\u591f\u9ad8\u6548\u5730\u5b9e\u73b0\u4e2d\u6587\u8fc7\u6ee4\uff0c\u6ee1\u8db3\u5404\u79cd\u590d\u6742\u7684\u6587\u672c\u5904\u7406\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bc6\u522b\u4e2d\u6587\u5b57\u7b26\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u8bc6\u522b\u4e2d\u6587\u5b57\u7b26\u3002\u901a\u8fc7\u5bfc\u5165<code>re<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u7f16\u5199\u5982\u4e0b\u4ee3\u7801\u6765\u5339\u914d\u4e2d\u6587\u5b57\u7b26\uff1a  <\/p>\n<pre><code class=\"language-python\">import re\n\ntext = &quot;Hello, \u4f60\u597d\uff01&quot;\nchinese_characters = re.findall(r&#39;[\\u4e00-\\u9fa5]&#39;, text)\nprint(chinese_characters)  # \u8f93\u51fa\uff1a[&#39;\u4f60&#39;, &#39;\u597d&#39;]\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u5229\u7528Unicode\u8303\u56f4\u6765\u5339\u914d\u6240\u6709\u5e38\u7528\u7684\u6c49\u5b57\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u7b80\u5355\u7684\u65b9\u6cd5\u53ef\u4ee5\u8fc7\u6ee4\u5b57\u7b26\u4e32\u4e2d\u7684\u4e2d\u6587\u5b57\u7b26\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u5b57\u7b26\u4e32\u7684<code>join()<\/code>\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u8fc7\u6ee4\u5b57\u7b26\u4e32\u4e2d\u7684\u4e2d\u6587\u5b57\u7b26\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">text = &quot;Hello, \u4f60\u597d\uff01&quot;\nfiltered_text = &#39;&#39;.join([char for char in text if not re.search(r&#39;[\\u4e00-\\u9fa5]&#39;, char)])\nprint(filtered_text)  # \u8f93\u51fa\uff1aHello, \uff01\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4f1a\u4fdd\u7559\u975e\u4e2d\u6587\u5b57\u7b26\uff0c\u5220\u9664\u6240\u6709\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u5904\u7406\u6587\u4ef6\u65f6\u8fc7\u6ee4\u4e2d\u6587\u5185\u5bb9\uff1f<\/strong><br \/>\u53ef\u4ee5\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\uff0c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fc7\u6ee4\u6bcf\u884c\u4e2d\u7684\u4e2d\u6587\u5b57\u7b26\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import re\n\nwith open(&#39;example.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:\n    for line in file:\n        filtered_line = re.sub(r&#39;[\\u4e00-\\u9fa5]&#39;, &#39;&#39;, line)\n        print(filtered_line)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u5904\u7406\u6587\u4ef6\u4e2d\u7684\u4e2d\u6587\u5185\u5bb9\uff0c\u4fdd\u7559\u5176\u4ed6\u5b57\u7b26\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8fdb\u884c\u4e2d\u6587\u8fc7\u6ee4\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5229\u7528\u5b57\u7b26\u4e32\u5e93\u3001\u4ee5\u53ca\u501f\u52a9\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5e93\u7b49 [&hellip;]","protected":false},"author":3,"featured_media":994377,"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\/994365"}],"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=994365"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/994365\/revisions"}],"predecessor-version":[{"id":994379,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/994365\/revisions\/994379"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/994377"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=994365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=994365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=994365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}