{"id":931738,"date":"2024-12-26T17:43:28","date_gmt":"2024-12-26T09:43:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/931738.html"},"modified":"2024-12-26T17:43:30","modified_gmt":"2024-12-26T09:43:30","slug":"python-%e5%a6%82%e4%bd%95%e5%8c%b9%e9%85%8d%e4%b8%ad%e6%96%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/931738.html","title":{"rendered":"python \u5982\u4f55\u5339\u914d\u4e2d\u6587"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065915\/6ff0ebc3-1993-4f2b-b121-6fa0553547eb.webp\" alt=\"python \u5982\u4f55\u5339\u914d\u4e2d\u6587\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5339\u914d\u4e2d\u6587\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001unicode\u7f16\u7801\u3001\u7b2c\u4e09\u65b9\u5e93\uff08\u5982jieba\uff09\u3002\u5728\u6b64\u57fa\u7840\u4e0a\uff0c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u6700\u5e38\u89c1\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5<\/strong>\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u4f9b\u4e86\u4e00\u79cd\u7075\u6d3b\u7684\u65b9\u5f0f\u6765\u5b9a\u4e49\u548c\u5339\u914d\u5b57\u7b26\u4e32\u6a21\u5f0f\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>re<\/code>\u6a21\u5757\u6765\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u8981\u5339\u914d\u4e2d\u6587\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528Unicode\u8303\u56f4[\\u4e00-\\u9fa5]\uff0c\u8fd9\u6db5\u76d6\u4e86\u5927\u90e8\u5206\u5e38\u7528\u7684\u6c49\u5b57\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython\u4e2d\u5339\u914d\u4e2d\u6587\u7684\u65b9\u6cd5\u548c\u5b9e\u8df5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u4e2d\u6587<\/p>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u6a21\u5f0f\u5339\u914d\u5de5\u5177\uff0c\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>re<\/code>\u6a21\u5757\u6765\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u4e2d\u6587\u5339\u914d\u3002\u5bf9\u4e8e\u4e2d\u6587\u5b57\u7b26\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528Unicode\u8303\u56f4[\\u4e00-\\u9fa5]\u6765\u5b9a\u4e49\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u4e2d\u6587<\/li>\n<\/ol>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u6765\u5339\u914d\u4e2d\u6587\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u4e2d\u6587\u7684\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>text = &quot;Hello, \u4e16\u754c\uff01\u8fd9\u662f\u4e00\u4e2a\u6d4b\u8bd5\u3002&quot;<\/p>\n<h2><strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u4e2d\u6587<\/strong><\/h2>\n<p>pattern = re.compile(r&#39;[\\u4e00-\\u9fa5]+&#39;)<\/p>\n<p>chinese_characters = pattern.findall(text)<\/p>\n<p>print(chinese_characters)  # \u8f93\u51fa\uff1a[&#39;\u4e16\u754c&#39;, &#39;\u8fd9\u662f\u4e00\u4e2a\u6d4b\u8bd5&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>re.compile()<\/code>\u51fd\u6570\u7528\u4e8e\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff0c<code>findall()<\/code>\u65b9\u6cd5\u7528\u4e8e\u8fd4\u56de\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u5339\u914d\u7684\u5b50\u4e32\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u5339\u914d\u4e2d\u6587\u7684\u5b8c\u6574\u53e5\u5b50<\/li>\n<\/ol>\n<p><p>\u5982\u679c\u9700\u8981\u5339\u914d\u5b8c\u6574\u7684\u4e2d\u6587\u53e5\u5b50\u800c\u4e0d\u4ec5\u4ec5\u662f\u5355\u4e2a\u6c49\u5b57\uff0c\u53ef\u4ee5\u8c03\u6574\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff0c\u4f7f\u5176\u5339\u914d\u5305\u62ec\u6807\u70b9\u7b26\u53f7\u5728\u5185\u7684\u5b8c\u6574\u4e2d\u6587\u53e5\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = re.compile(r&#39;[\\u4e00-\\u9fa5\uff0c\u3002\uff01\uff1f]+&#39;)<\/p>\n<p>chinese_sentences = pattern.findall(text)<\/p>\n<p>print(chinese_sentences)  # \u8f93\u51fa\uff1a[&#39;\u4e16\u754c\uff01\u8fd9\u662f\u4e00\u4e2a\u6d4b\u8bd5\u3002&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u66f4\u51c6\u786e\u5730\u63d0\u53d6\u5305\u542b\u6807\u70b9\u7b26\u53f7\u7684\u5b8c\u6574\u4e2d\u6587\u53e5\u5b50\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528unicode\u7f16\u7801\u5339\u914d\u4e2d\u6587<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\u4e4b\u5916\uff0c\u4e86\u89e3Unicode\u7f16\u7801\u4e5f\u662f\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u7684\u57fa\u7840\u3002\u5728Python\u4e2d\uff0c\u5b57\u7b26\u4e32\u901a\u5e38\u662f\u4ee5Unicode\u683c\u5f0f\u5b58\u50a8\u7684\uff0c\u8fd9\u4f7f\u5f97\u5904\u7406\u591a\u8bed\u8a00\u6587\u672c\uff08\u5305\u62ec\u4e2d\u6587\uff09\u66f4\u52a0\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<ol>\n<li>Unicode\u7f16\u7801\u8303\u56f4<\/li>\n<\/ol>\n<p><p>\u4e2d\u6587\u5b57\u7b26\u7684Unicode\u7f16\u7801\u8303\u56f4\u4e3b\u8981\u5728[\\u4e00-\\u9fa5]\u4e4b\u95f4\uff0c\u638c\u63e1\u8fd9\u4e00\u70b9\u6709\u52a9\u4e8e\u7406\u89e3\u548c\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;\u4f60\u597d&quot;<\/p>\n<p>for char in text:<\/p>\n<p>    print(f&quot;{char} \u7684Unicode\u7f16\u7801\u4e3a\uff1a{ord(char)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528\u6761\u4ef6\u5224\u65ad\u5339\u914d\u4e2d\u6587<\/li>\n<\/ol>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u9010\u4e2a\u5b57\u7b26\u5730\u5224\u65ad\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b\u4e2d\u6587\u5b57\u7b26\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u5224\u65ad\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def cont<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ns_chinese(text):<\/p>\n<p>    for char in text:<\/p>\n<p>        if &#39;\\u4e00&#39; &lt;= char &lt;= &#39;\\u9fa5&#39;:<\/p>\n<p>            return True<\/p>\n<p>    return False<\/p>\n<p>result = contains_chinese(&quot;Hello, \u4e16\u754c&quot;)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1aTrue<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u4e0d\u5982\u6b63\u5219\u8868\u8fbe\u5f0f\u7b80\u6d01\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u66f4\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff08jieba\uff09\u8fdb\u884c\u4e2d\u6587\u5206\u8bcd<\/p>\n<\/p>\n<p><p>\u5728\u6587\u672c\u5904\u7406\u4e2d\uff0c\u5c24\u5176\u662f\u81ea\u7136\u8bed\u8a00\u5904\u7406\u9886\u57df\uff0c\u4e2d\u6587\u5206\u8bcd\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u6b65\u9aa4\u3002jieba\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u4e2d\u6587\u5206\u8bcd\u5e93\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u5904\u7406\u4e2d\u6587\u6587\u672c\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5jieba\u5e93<\/li>\n<\/ol>\n<p><p>\u5728\u4f7f\u7528jieba\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5b89\u88c5\u8be5\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install jieba<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528jieba\u8fdb\u884c\u4e2d\u6587\u5206\u8bcd<\/li>\n<\/ol>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528jieba\u5bf9\u4e2d\u6587\u6587\u672c\u8fdb\u884c\u5206\u8bcd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import jieba<\/p>\n<p>text = &quot;\u8fd9\u662f\u4e00\u4e2a\u7528\u4e8e\u6d4b\u8bd5\u7684\u4e2d\u6587\u53e5\u5b50\u3002&quot;<\/p>\n<h2><strong>\u4f7f\u7528jieba\u8fdb\u884c\u5206\u8bcd<\/strong><\/h2>\n<p>segments = jieba.cut(text, cut_all=False)<\/p>\n<p>print(&quot;\u7cbe\u786e\u6a21\u5f0f: &quot; + &quot;\/ &quot;.join(segments))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>jieba\u7684\u5176\u4ed6\u529f\u80fd<\/li>\n<\/ol>\n<p><p>jieba\u4e0d\u4ec5\u53ef\u4ee5\u8fdb\u884c\u4e2d\u6587\u5206\u8bcd\uff0c\u8fd8\u652f\u6301\u5173\u952e\u8bcd\u63d0\u53d6\u3001\u8bcd\u6027\u6807\u6ce8\u7b49\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u63d0\u53d6\u5173\u952e\u8bcd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import jieba.analyse<\/p>\n<p>text = &quot;Python\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u9002\u5408\u6570\u636e\u5206\u6790\u4e0e<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u3002&quot;<\/p>\n<h2><strong>\u63d0\u53d6\u5173\u952e\u8bcd<\/strong><\/h2>\n<p>keywords = jieba.analyse.extract_tags(text, topK=3, withWeight=False)<\/p>\n<p>print(&quot;\u5173\u952e\u8bcd: &quot; + &quot;, &quot;.join(keywords))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3\u4e0e\u5b9e\u8df5\u5efa\u8bae<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5339\u914d\u4e2d\u6587\u5b57\u7b26\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u9009\u62e9\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u7075\u6d3b\u4e14\u9ad8\u6548\u7684\u9009\u62e9\uff0c\u9002\u5408\u5927\u591a\u6570\u7b80\u5355\u7684\u5339\u914d\u4efb\u52a1\u3002\u800c\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u6587\u672c\u5904\u7406\u4efb\u52a1\uff0c\u7ed3\u5408unicode\u7f16\u7801\u548cjieba\u5e93\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u52a0\u7ec6\u81f4\u7684\u4e2d\u6587\u6587\u672c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u8df5\u4e2d\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u573a\u666f\u3002\u5982\u679c\u9700\u8981\u5feb\u901f\u3001\u7b80\u5355\u5730\u5339\u914d\u4e2d\u6587\u5b57\u7b26\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u9996\u9009\uff1b\u800c\u5f53\u9700\u8981\u8fdb\u884c\u6df1\u5ea6\u7684\u6587\u672c\u5206\u6790\u548c\u5904\u7406\u65f6\uff0cjieba\u5e93\u5219\u663e\u5f97\u975e\u5e38\u6709\u7528\u3002\u901a\u8fc7\u5408\u7406\u7ec4\u5408\u8fd9\u4e9b\u5de5\u5177\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u89e3\u51b3\u4e2d\u6587\u5339\u914d\u548c\u5904\u7406\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6709\u6548\u5904\u7406\u548c\u5339\u914d\u4e2d\u6587\u5b57\u7b26\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5757<code>re<\/code>\u6765\u5339\u914d\u4e2d\u6587\u5b57\u7b26\u3002\u901a\u8fc7\u8bbe\u7f6e\u9002\u5f53\u7684\u6a21\u5f0f\uff0c\u5982<code>[\\u4e00-\\u9fa5]<\/code>\uff0c\u53ef\u4ee5\u7cbe\u786e\u5339\u914d\u6240\u6709\u6c49\u5b57\u3002\u4f7f\u7528<code>re.findall()<\/code>\u53ef\u4ee5\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u4e2d\u6587\u90e8\u5206\uff0c\u4fbf\u4e8e\u540e\u7eed\u5904\u7406\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u4e2d\u7684\u54ea\u4e9b\u5e93\u53ef\u4ee5\u66f4\u597d\u5730\u5904\u7406\u4e2d\u6587\u6587\u672c\uff1f<\/strong><br \/>\u9664\u4e86\u5185\u7f6e\u7684<code>re<\/code>\u6a21\u5757\u5916\uff0c<code>jieba<\/code>\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u4e2d\u6587\u5206\u8bcd\u5e93\uff0c\u80fd\u591f\u6709\u6548\u5730\u5c06\u4e2d\u6587\u53e5\u5b50\u5206\u5272\u6210\u8bcd\u6c47\u3002\u6b64\u5916\uff0c<code>pandas<\/code>\u548c<code>numpy<\/code>\u4e5f\u53ef\u4ee5\u5e2e\u52a9\u5904\u7406\u4e2d\u6587\u6570\u636e\uff0c\u5c24\u5176\u662f\u5728\u6570\u636e\u5206\u6790\u548c\u7edf\u8ba1\u65f6\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5339\u914d\u4e2d\u6587\u65f6\uff0c\u5982\u4f55\u5904\u7406\u6587\u672c\u7f16\u7801\u95ee\u9898\uff1f<\/strong><br \/>\u786e\u4fdd\u5728\u5904\u7406\u4e2d\u6587\u6587\u672c\u65f6\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u5982UTF-8\u3002\u5982\u679c\u6587\u672c\u4ee5\u5176\u4ed6\u7f16\u7801\u683c\u5f0f\uff08\u5982GBK\uff09\u5b58\u50a8\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5339\u914d\u95ee\u9898\u3002\u5728\u8bfb\u53d6\u6587\u4ef6\u65f6\uff0c\u9700\u6307\u5b9a\u6b63\u786e\u7684\u7f16\u7801\u4ee5\u907f\u514d\u4e71\u7801\u548c\u5339\u914d\u9519\u8bef\u3002\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>encoding=&#39;utf-8&#39;<\/code>\u53c2\u6570\u6765\u786e\u4fdd\u4ee5\u6b63\u786e\u7684\u7f16\u7801\u6253\u5f00\u6587\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5339\u914d\u4e2d\u6587\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001unicode\u7f16\u7801\u3001\u7b2c\u4e09\u65b9\u5e93\uff08\u5982jieba\uff09\u3002\u5728\u6b64\u57fa\u7840\u4e0a\uff0c\u4f7f\u7528\u6b63 [&hellip;]","protected":false},"author":3,"featured_media":931741,"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\/931738"}],"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=931738"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/931738\/revisions"}],"predecessor-version":[{"id":931745,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/931738\/revisions\/931745"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/931741"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=931738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=931738"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=931738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}