{"id":1170729,"date":"2025-01-15T16:26:09","date_gmt":"2025-01-15T08:26:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1170729.html"},"modified":"2025-01-15T16:26:12","modified_gmt":"2025-01-15T08:26:12","slug":"python3-9%e5%a6%82%e4%bd%95%e5%88%87%e4%b8%ad%e6%96%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1170729.html","title":{"rendered":"python3.9\u5982\u4f55\u5207\u4e2d\u6587"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26073304\/ed56b2e2-28d2-4b5e-86c7-10db1bb580ca.webp\" alt=\"python3.9\u5982\u4f55\u5207\u4e2d\u6587\" \/><\/p>\n<p><p> \u5728Python 3.9\u4e2d\u5207\u5206\u4e2d\u6587\u5b57\u7b26\u4e32\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c<strong>\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u5207\u7247\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001NLTK\u5e93\u3001jieba\u5e93\u7b49<\/strong>\u3002\u5176\u4e2d\uff0c<strong>jieba\u5e93\u662f\u6700\u63a8\u8350\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u56e0\u4e3a\u5b83\u4e13\u95e8\u7528\u4e8e\u4e2d\u6587\u5206\u8bcd\uff0c\u6548\u679c\u8f83\u597d\u4e14\u4f7f\u7528\u7b80\u5355<\/strong>\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python 3.9\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u5207\u5206\u4e2d\u6587\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b57\u7b26\u4e32\u5207\u7247\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u5207\u7247\u662fPython\u5185\u7f6e\u7684\u529f\u80fd\u4e4b\u4e00\uff0c\u53ef\u4ee5\u76f4\u63a5\u7528\u6765\u5207\u5206\u5b57\u7b26\u4e32\u3002\u5bf9\u4e8e\u4e2d\u6587\u5b57\u7b26\u4e32\uff0c\u5207\u7247\u65b9\u6cd5\u540c\u6837\u9002\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>text = &quot;\u8fd9\u662f\u4e00\u4e2a\u4e2d\u6587\u5b57\u7b26\u4e32&quot;<\/p>\n<p>print(text[0:2])  # \u8f93\u51fa: \u8fd9\u662f<\/p>\n<p>print(text[2:5])  # \u8f93\u51fa: \u4e00\u4e2a\u4e2d<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u5207\u7247\u9002\u7528\u4e8e\u9700\u8981\u6309\u56fa\u5b9a\u957f\u5ea6\u5207\u5206\u7684\u60c5\u51b5\uff0c\u4f46\u5b83\u65e0\u6cd5\u667a\u80fd\u5730\u8bc6\u522b\u4e2d\u6587\u8bcd\u6c47\u8fb9\u754c\uff0c\u9002\u7528\u8303\u56f4\u6709\u9650\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6587\u672c\u5904\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u5339\u914d\u7279\u5b9a\u6a21\u5f0f\u7684\u5b57\u7b26\u4e32\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u5207\u5206\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>text = &quot;\u8fd9\u662f\u4e00\u4e2a\u4e2d\u6587\u5b57\u7b26\u4e32&quot;<\/p>\n<p>pattern = re.compile(r&#39;.{2}&#39;)  # \u6bcf\u4e24\u4e2a\u5b57\u7b26\u5207\u5206\u4e00\u6b21<\/p>\n<p>result = pattern.findall(text)<\/p>\n<p>print(result)  # \u8f93\u51fa: [&#39;\u8fd9\u662f&#39;, &#39;\u4e00\u4e2a&#39;, &#39;\u4e2d\u6587&#39;, &#39;\u5b57\u7b26\u4e32&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u867d\u7136\u7075\u6d3b\uff0c\u4f46\u5bf9\u4e8e\u590d\u6742\u7684\u4e2d\u6587\u5206\u8bcd\u4efb\u52a1\uff0c\u53ef\u80fd\u4e0d\u5982\u4e13\u95e8\u7684\u5206\u8bcd\u5e93\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001NLTK\u5e93<\/h3>\n<\/p>\n<p><p>NLTK\uff08Natural Language Toolkit\uff09\u662f\u4e00\u4e2a\u7528\u4e8e\u5904\u7406\u81ea\u7136\u8bed\u8a00\u6587\u672c\u7684\u5e93\uff0c\u867d\u7136\u4e3b\u8981\u7528\u4e8e\u82f1\u6587\u6587\u672c\u5904\u7406\uff0c\u4f46\u5b83\u4e5f\u6709\u4e00\u4e9b\u529f\u80fd\u53ef\u4ee5\u7528\u6765\u5904\u7406\u4e2d\u6587\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import nltk<\/p>\n<p>from nltk.tokenize import word_tokenize<\/p>\n<h2><strong>\u4e0b\u8f7d\u4e2d\u6587\u5206\u8bcd\u6570\u636e\u5305<\/strong><\/h2>\n<p>nltk.download(&#39;punkt&#39;)<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>text = &quot;\u8fd9\u662f\u4e00\u4e2a\u4e2d\u6587\u5b57\u7b26\u4e32&quot;<\/p>\n<p>tokens = word_tokenize(text)<\/p>\n<p>print(tokens)  # \u8f93\u51fa: [&#39;\u8fd9\u662f&#39;, &#39;\u4e00\u4e2a&#39;, &#39;\u4e2d\u6587&#39;, &#39;\u5b57\u7b26\u4e32&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>NLTK\u7684\u4e2d\u6587\u5206\u8bcd\u6548\u679c\u8f83\u4e3a\u57fa\u7840\uff0c\u9002\u5408\u5165\u95e8\u5b66\u4e60\u548c\u7b80\u5355\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001jieba\u5e93<\/h3>\n<\/p>\n<p><p>jieba\u5e93\u662f\u4e13\u4e3a\u4e2d\u6587\u5206\u8bcd\u8bbe\u8ba1\u7684\uff0c\u6548\u679c\u6700\u597d\u4e14\u4f7f\u7528\u6700\u4e3a\u5e7f\u6cdb\u3002jieba\u63d0\u4f9b\u4e86\u591a\u79cd\u5206\u8bcd\u6a21\u5f0f\uff0c\u5305\u62ec\u7cbe\u786e\u6a21\u5f0f\u3001\u5168\u6a21\u5f0f\u548c\u641c\u7d22\u5f15\u64ce\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import jieba<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>text = &quot;\u8fd9\u662f\u4e00\u4e2a\u4e2d\u6587\u5b57\u7b26\u4e32&quot;<\/p>\n<h2><strong>\u7cbe\u786e\u6a21\u5f0f<\/strong><\/h2>\n<p>words = jieba.cut(text, cut_all=False)<\/p>\n<p>print(&quot;\/&quot;.join(words))  # \u8f93\u51fa: \u8fd9\u662f\/\u4e00\u4e2a\/\u4e2d\u6587\/\u5b57\u7b26\u4e32<\/p>\n<h2><strong>\u5168\u6a21\u5f0f<\/strong><\/h2>\n<p>words = jieba.cut(text, cut_all=True)<\/p>\n<p>print(&quot;\/&quot;.join(words))  # \u8f93\u51fa: \u8fd9\u662f\/\u662f\/\u4e00\u4e2a\/\u4e2d\u6587\/\u5b57\/\u7b26\/\u4e32<\/p>\n<h2><strong>\u641c\u7d22\u5f15\u64ce\u6a21\u5f0f<\/strong><\/h2>\n<p>words = jieba.cut_for_search(text)<\/p>\n<p>print(&quot;\/&quot;.join(words))  # \u8f93\u51fa: \u8fd9\u662f\/\u662f\/\u4e00\u4e2a\/\u4e2d\u6587\/\u5b57\u7b26\u4e32\/\u7b26\u4e32<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>jieba\u5e93\u4e0d\u4ec5\u80fd\u5904\u7406\u7b80\u5355\u7684\u5206\u8bcd\u9700\u6c42\uff0c\u8fd8\u652f\u6301\u7528\u6237\u81ea\u5b9a\u4e49\u8bcd\u5178\uff0c\u5e76\u80fd\u8fdb\u884c\u8bcd\u6027\u6807\u6ce8\uff0c\u662f\u5904\u7406\u4e2d\u6587\u6587\u672c\u7684\u6700\u4f73\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python 3.9\u4e2d\u5207\u5206\u4e2d\u6587\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c<strong>\u5305\u62ec\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u5207\u7247\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001NLTK\u5e93\u548cjieba\u5e93<\/strong>\u3002\u5176\u4e2d\uff0c<strong>jieba\u5e93\u7531\u4e8e\u5176\u4e13\u6ce8\u4e8e\u4e2d\u6587\u5206\u8bcd\u4e14\u6548\u679c\u663e\u8457\uff0c\u6700\u4e3a\u63a8\u8350<\/strong>\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5b57\u7b26\u4e32\u5207\u7247\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u4f46\u5bf9\u4e8e\u590d\u6742\u7684\u4e2d\u6587\u6587\u672c\u5904\u7406\u4efb\u52a1\uff0cjieba\u5e93\u65e0\u7591\u662f\u6700\u4f73\u9009\u62e9\u3002\u901a\u8fc7\u5408\u7406\u9009\u62e9\u548c\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5b8c\u6210\u4e2d\u6587\u5b57\u7b26\u4e32\u7684\u5207\u5206\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python 3.9\u4e2d\u5904\u7406\u4e2d\u6587\u5b57\u7b26\uff1f<\/strong><br \/>\u5728Python 3.9\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Unicode\u7f16\u7801\u6765\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u3002\u786e\u4fdd\u5728\u4ee3\u7801\u6587\u4ef6\u7684\u5f00\u5934\u6dfb\u52a0<code># -*- coding: utf-8 -*-<\/code>\uff0c\u8fd9\u6837Python\u5c31\u80fd\u6b63\u786e\u8bc6\u522b\u4e2d\u6587\u5b57\u7b26\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u8f93\u51fa\u4e2d\u6587\u65f6\uff0c\u8bf7\u786e\u4fdd\u7ec8\u7aef\u6216\u63a7\u5236\u53f0\u652f\u6301UTF-8\u7f16\u7801\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python 3.9\u4e2d\u8bfb\u53d6\u5305\u542b\u4e2d\u6587\u7684\u6587\u4ef6\uff1f<\/strong><br \/>\u8bfb\u53d6\u5305\u542b\u4e2d\u6587\u7684\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\uff0c\u5e76\u6307\u5b9a\u7f16\u7801\u4e3a<code>utf-8<\/code>\u3002\u4f8b\u5982\uff0c<code>with open(&#39;\u6587\u4ef6\u540d.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as f:<\/code>\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u56e0\u7f16\u7801\u4e0d\u5339\u914d\u800c\u5bfc\u81f4\u7684\u9519\u8bef\u3002<\/p>\n<p><strong>\u5728Python 3.9\u4e2d\u5982\u4f55\u8fdb\u884c\u4e2d\u6587\u5b57\u7b26\u4e32\u7684\u6bd4\u8f83\u548c\u6392\u5e8f\uff1f<\/strong><br \/>\u5728Python 3.9\u4e2d\uff0c\u4e2d\u6587\u5b57\u7b26\u4e32\u53ef\u4ee5\u76f4\u63a5\u8fdb\u884c\u6bd4\u8f83\u548c\u6392\u5e8f\u3002\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>sorted()<\/code>\u51fd\u6570\u8fdb\u884c\u6392\u5e8f\uff0c\u6ce8\u610f\u5982\u679c\u9700\u8981\u6309\u7167\u62fc\u97f3\u6216\u5176\u4ed6\u89c4\u5219\u6392\u5e8f\uff0c\u53ef\u4ee5\u501f\u52a9<code>pypinyin<\/code>\u5e93\u8fdb\u884c\u8f6c\u6362\u540e\u518d\u6392\u5e8f\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u4e2d\u6587\u5b57\u7b26\u4e32\u6309\u7167\u7279\u5b9a\u7684\u903b\u8f91\u8fdb\u884c\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python 3.9\u4e2d\u5207\u5206\u4e2d\u6587\u5b57\u7b26\u4e32\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u5207\u7247\u65b9\u6cd5\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001NLTK\u5e93\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1170739,"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\/1170729"}],"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=1170729"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1170729\/revisions"}],"predecessor-version":[{"id":1170740,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1170729\/revisions\/1170740"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1170739"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1170729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1170729"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1170729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}