{"id":1001788,"date":"2024-12-27T10:03:17","date_gmt":"2024-12-27T02:03:17","guid":{"rendered":""},"modified":"2024-12-27T10:03:19","modified_gmt":"2024-12-27T02:03:19","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e6%8f%90%e5%8f%96%e4%b8%ad%e6%96%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1001788.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u63d0\u53d6\u4e2d\u6587"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075821\/147d5bbf-c2d5-4769-90d1-6f312807f6a7.webp\" alt=\"python\u4e2d\u5982\u4f55\u63d0\u53d6\u4e2d\u6587\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u63d0\u53d6\u4e2d\u6587\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u548c\u5de5\u5177\u3002<strong>\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u7ed3\u5408\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5e93\uff08\u5982jieba\uff09\u3001\u5229\u7528Unicode\u7f16\u7801\u8303\u56f4\u6765\u5339\u914d\u4e2d\u6587\u5b57\u7b26<\/strong>\u3002\u5176\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u975e\u5e38\u9ad8\u6548\u548c\u7075\u6d3b\u7684\u5de5\u5177\uff0c\u9002\u5408\u5728\u5904\u7406\u5b57\u7b26\u4e32\u65f6\u8fdb\u884c\u6a21\u5f0f\u5339\u914d\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u4e2d\u6587\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u4e2d\u6587<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\uff08Regular Expressions\uff09\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u5b9a\u6a21\u5f0f\u3002\u5728Python\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u901a\u8fc7<code>re<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u548c\u5bfc\u5165re\u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>re<\/code>\u6a21\u5757\u662fPython\u5185\u7f6e\u6a21\u5757\uff0c\u65e0\u9700\u989d\u5916\u5b89\u88c5\uff0c\u53ef\u4ee5\u76f4\u63a5\u5bfc\u5165\u4f7f\u7528\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5bfc\u5165\u7684\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u57fa\u7840\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u8981\u63d0\u53d6\u4e2d\u6587\u5b57\u7b26\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u5339\u914d\u4e2d\u6587\u5b57\u7b26\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u3002\u4e2d\u6587\u5b57\u7b26\u7684Unicode\u7f16\u7801\u8303\u56f4\u662f<code>\\u4e00-\\u9fa5<\/code>\u3002\u56e0\u6b64\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5199\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = re.compile(r&#39;[\\u4e00-\\u9fa5]+&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u65b9\u6cd5<\/strong>\uff1a\u901a\u8fc7<code>re.findall()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u51fa\u6240\u6709\u5339\u914d\u7684\u6a21\u5f0f\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">text = &quot;Python\u662f\u4e00\u79cd\u7f16\u7a0b\u8bed\u8a00\u3002&quot;<\/p>\n<p>chinese_characters = pattern.findall(text)<\/p>\n<p>print(chinese_characters)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<code>[&#39;\u662f\u4e00\u79cd\u7f16\u7a0b\u8bed\u8a00&#39;]<\/code><\/p>\n<\/p>\n<p><h4>3. \u8be6\u7ec6\u89e3\u6790<\/h4>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6a21\u5f0f<code>[\\u4e00-\\u9fa5]+<\/code>\u7528\u4e8e\u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u8fde\u7eed\u7684\u4e2d\u6587\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>[\\u4e00-\\u9fa5]<\/code>\uff1a\u5339\u914d\u5355\u4e2a\u4e2d\u6587\u5b57\u7b26\u3002<\/li>\n<li><code>+<\/code>\uff1a\u5339\u914d\u524d\u4e00\u4e2a\u5b57\u7b26\u4e00\u6b21\u6216\u591a\u6b21\u3002<\/li>\n<\/ul>\n<p><p>\u8fd9\u79cd\u6a21\u5f0f\u5728\u5904\u7406\u5305\u542b\u4e2d\u6587\u7684\u6587\u672c\u65f6\u975e\u5e38\u5b9e\u7528\uff0c\u5c24\u5176\u662f\u5f53\u9700\u8981\u4ece\u6df7\u5408\u6587\u672c\u4e2d\u63d0\u53d6\u4e2d\u6587\u7247\u6bb5\u65f6\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u7ed3\u5408jieba\u5e93\u8fdb\u884c\u4e2d\u6587\u5206\u8bcd<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e2d\u6587\u6587\u672c\u65f6\uff0c\u5e38\u9700\u8981\u5206\u8bcd\u4ee5\u4fbf\u4e8e\u8fdb\u4e00\u6b65\u5206\u6790\u3002<code>jieba<\/code>\u662f\u4e00\u4e2a\u5e7f\u6cdb\u4f7f\u7528\u7684\u4e2d\u6587\u5206\u8bcd\u5e93\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u548c\u5bfc\u5165jieba<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86<code>jieba<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install jieba<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\u8be5\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import jieba<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528jieba\u8fdb\u884c\u5206\u8bcd<\/h4>\n<\/p>\n<p><p><code>jieba<\/code>\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u63a5\u53e3\u6765\u8fdb\u884c\u4e2d\u6587\u5206\u8bcd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Python\u662f\u4e00\u79cd\u7f16\u7a0b\u8bed\u8a00\u3002&quot;<\/p>\n<p>words = jieba.lcut(text)<\/p>\n<p>print(words)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<code>[&#39;Python&#39;, &#39;\u662f&#39;, &#39;\u4e00\u79cd&#39;, &#39;\u7f16\u7a0b&#39;, &#39;\u8bed\u8a00&#39;, &#39;\u3002&#39;]<\/code><\/p>\n<\/p>\n<p><h4>3. \u8be6\u7ec6\u89e3\u6790<\/h4>\n<\/p>\n<p><p><code>jieba.lcut()<\/code>\u65b9\u6cd5\u5c06\u8f93\u5165\u6587\u672c\u8fdb\u884c\u5206\u8bcd\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u662f\u4e00\u4e2a\u8bcd\u3002<code>jieba<\/code>\u7684\u5206\u8bcd\u80fd\u529b\u975e\u5e38\u5f3a\u5927\uff0c\u652f\u6301\u81ea\u5b9a\u4e49\u8bcd\u5178\u3001\u5173\u952e\u8bcd\u63d0\u53d6\u7b49\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5229\u7528Unicode\u7f16\u7801\u8303\u56f4\u5339\u914d\u4e2d\u6587<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\u548c<code>jieba<\/code>\u5e93\u5916\uff0c\u4e86\u89e3Unicode\u7f16\u7801\u8303\u56f4\u7684\u77e5\u8bc6\u4e5f\u80fd\u5e2e\u52a9\u6211\u4eec\u624b\u52a8\u63d0\u53d6\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u4e8eUnicode\u7f16\u7801<\/h4>\n<\/p>\n<p><p>\u4e2d\u6587\u5b57\u7b26\u7684Unicode\u8303\u56f4\u901a\u5e38\u5728<code>\\u4e00<\/code>\u5230<code>\\u9fff<\/code>\u4e4b\u95f4\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5b57\u7b26\u7f16\u7801\u529f\u80fd\u6765\u7b5b\u9009\u6587\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def extract_chinese_characters(text):<\/p>\n<p>    return &#39;&#39;.join([char for char in text if &#39;\\u4e00&#39; &lt;= char &lt;= &#39;\\u9fff&#39;])<\/p>\n<p>text = &quot;Python\u662f\u4e00\u79cd\u7f16\u7a0b\u8bed\u8a00\u3002&quot;<\/p>\n<p>chinese_characters = extract_chinese_characters(text)<\/p>\n<p>print(chinese_characters)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<code>\u662f\u4e00\u79cd\u7f16\u7a0b\u8bed\u8a00<\/code><\/p>\n<\/p>\n<p><h4>2. \u8be6\u7ec6\u89e3\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5229\u7528\u5217\u8868\u89e3\u6790\u6765\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u68c0\u67e5\u5176\u662f\u5426\u5728\u4e2d\u6587\u5b57\u7b26\u7684Unicode\u8303\u56f4\u5185\uff0c\u5e76\u5c06\u5176\u52a0\u5165\u7ed3\u679c\u4e2d\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u9ad8\u6548\uff0c\u7279\u522b\u9002\u5408\u5904\u7406\u5c0f\u89c4\u6a21\u7684\u6587\u672c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u63d0\u53d6\u4e2d\u6587\u5b57\u7b26\uff0c<strong>\u6b63\u5219\u8868\u8fbe\u5f0f\u3001jieba\u5e93\u548cUnicode\u7f16\u7801\u8303\u56f4<\/strong>\u90fd\u662f\u975e\u5e38\u6709\u6548\u7684\u65b9\u6cd5\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u6570\u636e\u89c4\u6a21\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\uff1a\u9002\u5408\u7b80\u5355\u3001\u5feb\u901f\u7684\u4e2d\u6587\u5b57\u7b26\u63d0\u53d6\u3002<\/li>\n<li><strong>jieba\u5e93<\/strong>\uff1a\u5728\u9700\u8981\u5206\u8bcd\u7684\u60c5\u51b5\u4e0b\u975e\u5e38\u6709\u7528\u3002<\/li>\n<li><strong>Unicode\u7f16\u7801\u8303\u56f4<\/strong>\uff1a\u76f4\u63a5\u64cd\u4f5c\u5b57\u7b26\uff0c\u9002\u5408\u5feb\u901f\u8fc7\u6ee4\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528\u8fd9\u4e9b\u6280\u672f\uff0c\u60a8\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u548c\u5206\u6790\u4e2d\u6587\u6587\u672c\u3002\u65e0\u8bba\u662f\u6570\u636e\u6e05\u6d17\u3001\u6587\u672c\u5206\u6790\u8fd8\u662f\u81ea\u7136\u8bed\u8a00\u5904\u7406\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u80fd\u4e3a\u60a8\u63d0\u4f9b\u5f3a\u5927\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bc6\u522b\u548c\u63d0\u53d6\u4e2d\u6587\u5b57\u7b26\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u8bc6\u522b\u548c\u63d0\u53d6\u4e2d\u6587\u5b57\u7b26\u3002\u53ef\u4ee5\u4f7f\u7528<code>re<\/code>\u6a21\u5757\u4e2d\u7684<code>findall<\/code>\u51fd\u6570\uff0c\u7ed3\u5408\u9002\u5f53\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u4f8b\u5982<code>[\\u4e00-\\u9fa5]<\/code>\uff0c\u6765\u5339\u914d\u6240\u6709\u4e2d\u6587\u5b57\u7b26\u3002\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u5730\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u51fa\u4e2d\u6587\u5185\u5bb9\u3002<\/p>\n<p><strong>\u63d0\u53d6\u4e2d\u6587\u65f6\u5982\u4f55\u5904\u7406\u6df7\u5408\u6587\u672c\uff1f<\/strong><br \/>\u5f53\u6587\u672c\u4e2d\u5305\u542b\u4e2d\u6587\u548c\u5176\u4ed6\u8bed\u8a00\uff08\u5982\u82f1\u6587\u6216\u6570\u5b57\uff09\u65f6\uff0c\u53ef\u4ee5\u4f9d\u7136\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u63d0\u53d6\u4e2d\u6587\u3002\u6b63\u5219\u8868\u8fbe\u5f0f<code>[\\u4e00-\\u9fa5]+<\/code>\u53ef\u4ee5\u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u8fde\u7eed\u7684\u4e2d\u6587\u5b57\u7b26\uff0c\u4ece\u800c\u6709\u6548\u5730\u4ece\u6df7\u5408\u6587\u672c\u4e2d\u63d0\u53d6\u51fa\u4e2d\u6587\u90e8\u5206\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u63d0\u53d6\u4e2d\u6587\u5b57\u7b26\u6709\u4ec0\u4e48\u5e93\u53ef\u4ee5\u63a8\u8350\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528\u5185\u7f6e\u7684<code>re<\/code>\u6a21\u5757\uff0c\u8fd8\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982<code>jieba<\/code>\u8fdb\u884c\u4e2d\u6587\u5206\u8bcd\uff0c\u8fd9\u6837\u53ef\u4ee5\u66f4\u597d\u5730\u5904\u7406\u4e2d\u6587\u6587\u672c\uff0c\u5c24\u5176\u662f\u5728\u63d0\u53d6\u6709\u610f\u4e49\u7684\u8bcd\u6c47\u65f6\u3002\u6b64\u5916\uff0c<code>pandas<\/code>\u5e93\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u548c\u5206\u6790\u5305\u542b\u4e2d\u6587\u7684DataFrame\u6570\u636e\uff0c\u65b9\u4fbf\u8fdb\u884c\u6570\u636e\u63d0\u53d6\u548c\u6e05\u6d17\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u63d0\u53d6\u4e2d\u6587\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u548c\u5de5\u5177\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u7ed3\u5408\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5e93\uff08\u5982ji [&hellip;]","protected":false},"author":3,"featured_media":1001790,"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\/1001788"}],"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=1001788"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1001788\/revisions"}],"predecessor-version":[{"id":1001791,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1001788\/revisions\/1001791"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1001790"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1001788"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1001788"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1001788"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}