{"id":1007788,"date":"2024-12-27T10:56:51","date_gmt":"2024-12-27T02:56:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1007788.html"},"modified":"2024-12-27T10:56:54","modified_gmt":"2024-12-27T02:56:54","slug":"python-%e5%a6%82%e4%bd%95%e7%94%a8%e4%b8%ad%e6%96%87%e8%be%93%e5%87%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1007788.html","title":{"rendered":"python \u5982\u4f55\u7528\u4e2d\u6587\u8f93\u51fa"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083357\/c8e79479-b367-4619-86e9-543984a71152.webp\" alt=\"python \u5982\u4f55\u7528\u4e2d\u6587\u8f93\u51fa\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u4f7f\u7528\u4e2d\u6587\u8f93\u51fa\uff0c\u4e3b\u8981\u6d89\u53ca\u5230\u8bbe\u7f6e\u5b57\u7b26\u7f16\u7801\u3001\u4f7f\u7528\u5408\u9002\u7684\u7f16\u8f91\u5668\u548c\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u7684\u5e93\u3002\u9009\u62e9\u652f\u6301UTF-8\u7f16\u7801\u7684\u7f16\u8f91\u5668\u3001\u4f7f\u7528Python\u5185\u7f6e\u7684print\u51fd\u6570\u3001\u8bbe\u7f6e\u6587\u4ef6\u7f16\u7801\u4e3aUTF-8\uff0c\u662f\u786e\u4fddPython\u7a0b\u5e8f\u80fd\u591f\u6b63\u786e\u8f93\u51fa\u4e2d\u6587\u7684\u5173\u952e\u3002<\/strong>  <\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u8be6\u7ec6\u8bf4\u660e\uff0c\u6211\u4eec\u5c06\u4ece\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\u8fdb\u884c\u63a2\u8ba8\uff1aPython\u7f16\u7801\u8bbe\u7f6e\u3001Python\u7f16\u8f91\u5668\u9009\u62e9\u3001\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u7684\u5e93\u3001\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6cd5\u3001\u6700\u4f73\u5b9e\u8df5\u548c\u5e94\u7528\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001PYTHON\u7f16\u7801\u8bbe\u7f6e  <\/p>\n<\/p>\n<p><p>Python\u7684\u5b57\u7b26\u7f16\u7801\u8bbe\u7f6e\u5bf9\u4e8e\u4e2d\u6587\u8f93\u51fa\u81f3\u5173\u91cd\u8981\u3002Python 3\u9ed8\u8ba4\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u8fd9\u4f7f\u5f97\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u53d8\u5f97\u76f8\u5bf9\u7b80\u5355\u3002\u5728Python 2\u4e2d\uff0c\u9ed8\u8ba4\u7f16\u7801\u662fASCII\uff0c\u56e0\u6b64\u9700\u8981\u624b\u52a8\u8bbe\u7f6e\u4e3aUTF-8\u3002  <\/p>\n<\/p>\n<ol>\n<li>UTF-8\u7f16\u7801\u7684\u91cd\u8981\u6027<\/li>\n<\/ol>\n<p><p>UTF-8\u662f\u4e00\u79cd\u53ef\u53d8\u957f\u5ea6\u7684\u7f16\u7801\u65b9\u5f0f\uff0c\u80fd\u591f\u8868\u793a\u4efb\u610f\u5b57\u7b26\u96c6\u4e2d\u7684\u5b57\u7b26\u3002\u5bf9\u4e8e\u4e2d\u6587\u5b57\u7b26\uff0cUTF-8\u80fd\u591f\u6709\u6548\u5730\u8fdb\u884c\u7f16\u7801\u548c\u89e3\u7801\uff0c\u4fdd\u8bc1\u4e86\u4e2d\u6587\u5b57\u7b26\u5728Python\u7a0b\u5e8f\u4e2d\u7684\u6b63\u5e38\u663e\u793a\u3002  <\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u5982\u4f55\u8bbe\u7f6e\u7f16\u7801<\/li>\n<\/ol>\n<p><p>\u5728Python 3\u4e2d\uff0c\u901a\u5e38\u4e0d\u9700\u8981\u624b\u52a8\u8bbe\u7f6e\u7f16\u7801\uff0c\u56e0\u4e3a\u9ed8\u8ba4\u4f7f\u7528UTF-8\u3002\u4f46\u5728Python 2\u4e2d\uff0c\u53ef\u4ee5\u5728\u6587\u4ef6\u5934\u90e8\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\u6765\u8bbe\u7f6e\u7f16\u7801\uff1a  <\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-  <\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u884c\u4ee3\u7801\u544a\u8bc9Python\u89e3\u91ca\u5668\u4f7f\u7528UTF-8\u7f16\u7801\u6765\u8bfb\u53d6\u548c\u6267\u884c\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\u3002  <\/p>\n<\/p>\n<p><p>\u4e8c\u3001PYTHON\u7f16\u8f91\u5668\u9009\u62e9  <\/p>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u7f16\u8f91\u5668\u5bf9\u4e8e\u6b63\u786e\u5730\u5904\u7406\u548c\u663e\u793a\u4e2d\u6587\u5b57\u7b26\u4e5f\u975e\u5e38\u91cd\u8981\u3002  <\/p>\n<\/p>\n<ol>\n<li>\u652f\u6301UTF-8\u7f16\u7801\u7684\u7f16\u8f91\u5668<\/li>\n<\/ol>\n<p><p>\u5927\u591a\u6570\u73b0\u4ee3\u7f16\u8f91\u5668\uff0c\u5982Visual Studio Code\u3001PyCharm\u3001Sublime Text\u7b49\uff0c\u90fd\u652f\u6301UTF-8\u7f16\u7801\u3002\u8fd9\u4e9b\u7f16\u8f91\u5668\u4e0d\u4ec5\u53ef\u4ee5\u6b63\u786e\u663e\u793a\u4e2d\u6587\u5b57\u7b26\uff0c\u8fd8\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u4ee3\u7801\u8865\u5168\u548c\u8c03\u8bd5\u529f\u80fd\u3002  <\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u7f16\u8f91\u5668\u7684\u914d\u7f6e<\/li>\n<\/ol>\n<p><p>\u786e\u4fdd\u5728\u7f16\u8f91\u5668\u4e2d\u8bbe\u7f6e\u6587\u4ef6\u7f16\u7801\u4e3aUTF-8\uff0c\u4ee5\u907f\u514d\u7f16\u7801\u4e0d\u4e00\u81f4\u7684\u95ee\u9898\u3002\u5728\u7f16\u8f91\u5668\u7684\u8bbe\u7f6e\u4e2d\uff0c\u53ef\u4ee5\u9009\u62e9\u6587\u4ef6\u7f16\u7801\u7684\u9009\u9879\uff0c\u5c06\u5176\u8bbe\u7f6e\u4e3aUTF-8\u5373\u53ef\u3002  <\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u7684\u5e93  <\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u8bb8\u591a\u5e93\u53ef\u4ee5\u5e2e\u52a9\u5904\u7406\u4e2d\u6587\u5b57\u7b26\uff0c\u5c24\u5176\u662f\u5728\u8fdb\u884c\u81ea\u7136\u8bed\u8a00\u5904\u7406\u65f6\u3002  <\/p>\n<\/p>\n<ol>\n<li>jieba\u5e93<\/li>\n<\/ol>\n<p><p>jieba\u662f\u4e00\u4e2a\u4e2d\u6587\u5206\u8bcd\u5e93\uff0c\u5e7f\u6cdb\u7528\u4e8e\u81ea\u7136\u8bed\u8a00\u5904\u7406\u4efb\u52a1\u3002\u5b83\u80fd\u591f\u5c06\u4e00\u6bb5\u4e2d\u6587\u6587\u672c\u62c6\u5206\u6210\u8bcd\u8bed\uff0c\u4f7f\u5f97\u6587\u672c\u5206\u6790\u66f4\u52a0\u65b9\u4fbf\u3002  <\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import jieba  <\/p>\n<p>text = &quot;\u6211\u559c\u6b22\u5b66\u4e60Python\u7f16\u7a0b&quot;  <\/p>\n<p>words = jieba.lcut(text)  <\/p>\n<p>print(words)  <\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4e2d\u6587\u6587\u672c\u7684\u5904\u7406<\/li>\n<\/ol>\n<p><p>\u5728\u5904\u7406\u4e2d\u6587\u6587\u672c\u65f6\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u8fdb\u884c\u6570\u636e\u5206\u6790\uff0cmatplotlib\u548cseaborn\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u3002\u8fd9\u4e9b\u5e93\u90fd\u652f\u6301\u4e2d\u6587\u5b57\u7b26\u7684\u5904\u7406\uff0c\u53ea\u9700\u8981\u5728\u56fe\u8868\u4e2d\u8bbe\u7f6e\u5b57\u4f53\u4e3a\u652f\u6301\u4e2d\u6587\u7684\u5b57\u4f53\u5373\u53ef\u3002  <\/p>\n<\/p>\n<p><p>\u56db\u3001\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6cd5  <\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Python\u8f93\u51fa\u4e2d\u6587\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\uff0c\u5982\u4e71\u7801\u3001\u7f16\u7801\u9519\u8bef\u7b49\u3002  <\/p>\n<\/p>\n<ol>\n<li>\u4e71\u7801\u95ee\u9898<\/li>\n<\/ol>\n<p><p>\u4e71\u7801\u901a\u5e38\u662f\u7531\u4e8e\u7f16\u7801\u4e0d\u4e00\u81f4\u9020\u6210\u7684\u3002\u5728\u8bfb\u53d6\u6216\u8f93\u51fa\u4e2d\u6587\u6587\u672c\u65f6\uff0c\u786e\u4fdd\u4f7f\u7528\u76f8\u540c\u7684\u7f16\u7801\u683c\u5f0f\uff08\u5982UTF-8\uff09\u3002  <\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u7f16\u7801\u9519\u8bef<\/li>\n<\/ol>\n<p><p>\u5982\u679c\u51fa\u73b0\u7f16\u7801\u9519\u8bef\uff0c\u53ef\u4ee5\u68c0\u67e5\u6587\u4ef6\u5934\u90e8\u7684\u7f16\u7801\u8bbe\u7f6e\uff0c\u786e\u4fdd\u7f16\u8f91\u5668\u548cPython\u6587\u4ef6\u7684\u7f16\u7801\u4e00\u81f4\u3002\u6b64\u5916\uff0c\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570encode()\u548cdecode()\u8fdb\u884c\u7f16\u7801\u8f6c\u6362\u4e5f\u80fd\u89e3\u51b3\u90e8\u5206\u95ee\u9898\u3002  <\/p>\n<\/p>\n<p><p>\u4e94\u3001\u6700\u4f73\u5b9e\u8df5\u548c\u5e94\u7528\u5b9e\u4f8b  <\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fddPython\u7a0b\u5e8f\u80fd\u591f\u6b63\u786e\u8f93\u51fa\u548c\u5904\u7406\u4e2d\u6587\u5b57\u7b26\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u548c\u5e94\u7528\u5b9e\u4f8b\u3002  <\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/li>\n<\/ol>\n<p><p>\u5728\u8f93\u51fa\u4e2d\u6587\u5b57\u7b26\u65f6\uff0c\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002  <\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;\u5c0f\u660e&quot;  <\/p>\n<p>print(f&quot;\u6b22\u8fce{name}\u6765\u5230Python\u4e16\u754c\uff01&quot;)  <\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4e2d\u6587\u6587\u672c\u5206\u6790\u5b9e\u4f8b<\/li>\n<\/ol>\n<p><p>\u5728\u8fdb\u884c\u4e2d\u6587\u6587\u672c\u5206\u6790\u65f6\uff0c\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528jieba\u3001pandas\u7b49\u5e93\uff0c\u5bf9\u6587\u672c\u6570\u636e\u8fdb\u884c\u5206\u8bcd\u3001\u7edf\u8ba1\u548c\u53ef\u89c6\u5316\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5b9e\u4f8b\uff1a  <\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import jieba  <\/p>\n<p>import pandas as pd  <\/p>\n<p>text = &quot;Python\u662f\u4e00\u95e8\u975e\u5e38\u5f3a\u5927\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u9002\u5408\u6570\u636e\u5206\u6790\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u3002&quot;  <\/p>\n<p>words = jieba.lcut(text)  <\/p>\n<p>word_count = pd.Series(words).value_counts()  <\/p>\n<p>print(word_count)  <\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u8bbe\u7f6e\u548c\u5b9e\u8df5\uff0cPython\u7a0b\u5e8f\u80fd\u591f\u6b63\u786e\u8f93\u51fa\u4e2d\u6587\uff0c\u5e76\u8fdb\u884c\u590d\u6742\u7684\u6587\u672c\u5904\u7406\u548c\u5206\u6790\u3002\u65e0\u8bba\u662f\u521d\u5b66\u8005\u8fd8\u662f\u7ecf\u9a8c\u4e30\u5bcc\u7684\u5f00\u53d1\u8005\uff0c\u8fd9\u4e9b\u6280\u5de7\u90fd\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u5904\u7406\u548c\u8f93\u51fa\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8f93\u51fa\u4e2d\u6587\u5b57\u7b26\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u76f4\u63a5\u8f93\u51fa\u4e2d\u6587\u5b57\u7b26\uff0c\u53ea\u9700\u786e\u4fdd\u6e90\u4ee3\u7801\u6587\u4ef6\u7684\u7f16\u7801\u4e3aUTF-8\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u76f4\u63a5\u5199\u5165\u4e2d\u6587\u5b57\u7b26\u4e32\uff1a  <\/p>\n<pre><code class=\"language-python\">print(&quot;\u4f60\u597d\uff0c\u4e16\u754c\uff01&quot;)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u60a8\u7684\u7f16\u8f91\u5668\u652f\u6301UTF-8\u7f16\u7801\uff0c\u8fd9\u6837\u5c31\u80fd\u6b63\u786e\u663e\u793a\u4e2d\u6587\u8f93\u51fa\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u4e2d\u6587\u7f16\u7801\u95ee\u9898\uff1f<\/strong><br \/>\u5904\u7406\u4e2d\u6587\u7f16\u7801\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u6e90\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u3002\u786e\u4fdd\u60a8\u7684Python\u811a\u672c\u6587\u4ef6\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u5e76\u5728\u6587\u4ef6\u5f00\u5934\u6dfb\u52a0<code># -*- coding: utf-8 -*-<\/code>\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u56e0\u7f16\u7801\u4e0d\u5339\u914d\u5bfc\u81f4\u7684\u9519\u8bef\u3002\u6b64\u5916\uff0c\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\u65f6\uff0c\u4e5f\u8981\u6307\u5b9a\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u4ee5\u786e\u4fdd\u4e2d\u6587\u5b57\u7b26\u80fd\u591f\u6b63\u786e\u5904\u7406\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u4e2d\u6587\u8fdb\u884c\u8f93\u5165\u548c\u8f93\u51fa\u7684\u793a\u4f8b\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u901a\u8fc7<code>input()<\/code>\u51fd\u6570\u63a5\u6536\u4e2d\u6587\u8f93\u5165\uff0c\u5e76\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u8f93\u51fa\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">name = input(&quot;\u8bf7\u8f93\u5165\u60a8\u7684\u540d\u5b57\uff1a&quot;)\nprint(f&quot;\u4f60\u597d\uff0c{name}\uff01&quot;)\n<\/code><\/pre>\n<p>\u8fd0\u884c\u8fd9\u4e2a\u4ee3\u7801\u7247\u6bb5\u540e\uff0c\u7528\u6237\u53ef\u4ee5\u8f93\u5165\u4e2d\u6587\u540d\u5b57\uff0c\u7a0b\u5e8f\u4f1a\u6b63\u786e\u8f93\u51fa\u5305\u542b\u4e2d\u6587\u7684\u95ee\u5019\u8bed\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4f7f\u7528\u4e2d\u6587\u8f93\u51fa\uff0c\u4e3b\u8981\u6d89\u53ca\u5230\u8bbe\u7f6e\u5b57\u7b26\u7f16\u7801\u3001\u4f7f\u7528\u5408\u9002\u7684\u7f16\u8f91\u5668\u548c\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u7684\u5e93\u3002\u9009\u62e9\u652f\u6301UTF-8\u7f16 [&hellip;]","protected":false},"author":3,"featured_media":1007794,"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\/1007788"}],"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=1007788"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1007788\/revisions"}],"predecessor-version":[{"id":1007795,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1007788\/revisions\/1007795"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1007794"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1007788"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1007788"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1007788"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}