{"id":994745,"date":"2024-12-27T08:58:14","date_gmt":"2024-12-27T00:58:14","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/994745.html"},"modified":"2024-12-27T08:58:16","modified_gmt":"2024-12-27T00:58:16","slug":"python%e5%a6%82%e4%bd%95%e6%94%b9%e6%88%90%e4%b8%ad%e6%96%87%e5%bd%a2%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/994745.html","title":{"rendered":"python\u5982\u4f55\u6539\u6210\u4e2d\u6587\u5f62\u5f0f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071602\/8db1f182-cf3c-4a75-b71b-f0e347bd5bf4.webp\" alt=\"python\u5982\u4f55\u6539\u6210\u4e2d\u6587\u5f62\u5f0f\" \/><\/p>\n<p><p> <strong>\u8981\u5c06Python\u6539\u6210\u4e2d\u6587\u5f62\u5f0f\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b89\u88c5\u4e2d\u6587\u5e93\u3001\u8bbe\u7f6e\u4e2d\u6587\u7f16\u7801\u3001\u4f7f\u7528\u4e2d\u6587\u6ce8\u91ca\u4e0e\u53d8\u91cf\u540d\u3001\u4f7f\u7528\u4e2d\u6587\u6587\u6863\u7b49\u65b9\u5f0f\u6765\u5b9e\u73b0<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u91cd\u8981\u7684\u4e00\u70b9\u662f\u8bbe\u7f6e\u4e2d\u6587\u7f16\u7801\uff0c\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u80fd\u591f\u6b63\u786e\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e9b\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5\u4e2d\u6587\u5e93<\/p>\n<\/p>\n<p><p>Python\u6709\u8bb8\u591a\u652f\u6301\u4e2d\u6587\u5904\u7406\u7684\u5e93\uff0c\u5982jieba\uff08\u7528\u4e8e\u4e2d\u6587\u5206\u8bcd\uff09\u3001pypinyin\uff08\u7528\u4e8e\u6c49\u5b57\u8f6c\u62fc\u97f3\uff09\u7b49\u3002\u8fd9\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u5904\u7406\u4e2d\u6587\u6587\u672c\u65f6\u66f4\u52a0\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5jieba\u5e93<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install jieba<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5b89\u88c5pypinyin\u5e93<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install pypinyin<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u4e2d\u6587\u5904\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u8ba9\u4f60\u7684Python\u7a0b\u5e8f\u66f4\u52a0\u9002\u5e94\u4e2d\u6587\u73af\u5883\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u8bbe\u7f6e\u4e2d\u6587\u7f16\u7801<\/p>\n<\/p>\n<p><p>\u5728Python\u7a0b\u5e8f\u4e2d\uff0c\u786e\u4fdd\u6587\u4ef6\u7f16\u7801\u4e3aUTF-8\u662f\u5904\u7406\u4e2d\u6587\u7684\u5173\u952e\u3002\u901a\u5e38\u5728\u6587\u4ef6\u7684\u5f00\u5934\u52a0\u4e0a\u5982\u4e0b\u58f0\u660e\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\u6309\u7167UTF-8\u7f16\u7801\u6765\u8bfb\u53d6\u6587\u4ef6\uff0c\u4ece\u800c\u4fdd\u8bc1\u4e2d\u6587\u5b57\u7b26\u7684\u6b63\u786e\u663e\u793a\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u4e2d\u6587\u6ce8\u91ca\u4e0e\u53d8\u91cf\u540d<\/p>\n<\/p>\n<p><p>\u5728Python\u4ee3\u7801\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e2d\u6587\u6ce8\u91ca\u548c\u53d8\u91cf\u540d\u6765\u589e\u52a0\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u4eb2\u548c\u529b\u3002\u8fd9\u5bf9\u4e8e\u4e2d\u6587\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u975e\u5e38\u76f4\u89c2\u548c\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8fd9\u662f\u4e00\u4e2a\u4e2d\u6587\u6ce8\u91ca<\/p>\n<p>\u53d8\u91cf\u540d = &quot;\u8fd9\u662f\u4e00\u4e2a\u4e2d\u6587\u53d8\u91cf&quot;<\/p>\n<p>print(\u53d8\u91cf\u540d)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u4e2d\u6587\u6ce8\u91ca\u548c\u53d8\u91cf\u540d\u53ef\u4ee5\u8ba9\u4ee3\u7801\u66f4\u5bb9\u6613\u88ab\u4e2d\u6587\u80cc\u666f\u7684\u5f00\u53d1\u8005\u7406\u89e3\uff0c\u4f46\u540c\u65f6\u4e5f\u8981\u6ce8\u610f\u56e2\u961f\u4e2d\u662f\u5426\u6709\u975e\u4e2d\u6587\u80cc\u666f\u7684\u6210\u5458\uff0c\u4ee5\u514d\u9020\u6210\u6c9f\u901a\u969c\u788d\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u4e2d\u6587\u6587\u6863<\/p>\n<\/p>\n<p><p>\u5728\u7f16\u5199Python\u9879\u76ee\u65f6\uff0c\u7f16\u5199\u4e2d\u6587\u6587\u6863\u53ef\u4ee5\u5e2e\u52a9\u4e2d\u6587\u7528\u6237\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528\u4f60\u7684\u4ee3\u7801\u3002\u53ef\u4ee5\u4f7f\u7528Markdown\u6216\u5176\u4ed6\u6587\u6863\u5de5\u5177\u6765\u7f16\u5199\u4e2d\u6587\u6587\u6863\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528Markdown\u7f16\u5199\u6587\u6863<\/li>\n<\/ol>\n<p><p>Markdown\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6807\u8bb0\u8bed\u8a00\uff0c\u652f\u6301\u7b80\u5355\u7684\u6587\u672c\u683c\u5f0f\u548c\u6392\u7248\uff0c\u975e\u5e38\u9002\u5408\u7f16\u5199\u6587\u6863\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528Sphinx\u751f\u6210\u6587\u6863<\/li>\n<\/ol>\n<p><p>Sphinx\u662f\u4e00\u4e2a\u6587\u6863\u751f\u6210\u5de5\u5177\uff0c\u53ef\u4ee5\u5c06Python\u9879\u76ee\u7684\u6ce8\u91ca\u751f\u6210HTML\u3001PDF\u7b49\u683c\u5f0f\u7684\u6587\u6863\u3002\u901a\u8fc7Sphinx\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u652f\u6301\u4e2d\u6587\u7684\u6587\u6863\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5904\u7406\u4e2d\u6587\u8f93\u5165\u4e0e\u8f93\u51fa<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e2d\u6587\u8f93\u5165\u8f93\u51fa\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u7ec8\u7aef\u6216IDE\u652f\u6301\u4e2d\u6587\u5b57\u7b26\u663e\u793a\uff0c\u5e76\u6839\u636e\u9700\u8981\u8fdb\u884c\u7f16\u7801\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f93\u5165\u4e2d\u6587<\/p>\n<p>\u7528\u6237\u8f93\u5165 = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e9b\u4e2d\u6587\uff1a&quot;)<\/p>\n<p>print(&quot;\u60a8\u8f93\u5165\u7684\u662f\uff1a&quot;, \u7528\u6237\u8f93\u5165)<\/p>\n<h2><strong>\u8f93\u51fa\u4e2d\u6587<\/strong><\/h2>\n<p>print(&quot;\u8fd9\u662f\u4e00\u4e2a\u4e2d\u6587\u8f93\u51fa\u793a\u4f8b&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u786e\u4fdd\u4f60\u7684\u7ec8\u7aef\u6216IDE\u8bbe\u7f6e\u4e3a\u652f\u6301UTF-8\u7f16\u7801\uff0c\u4ee5\u4fbf\u6b63\u786e\u663e\u793a\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u4f7f\u7528\u4e2d\u6587\u754c\u9762\u5e93<\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u6b63\u5728\u5f00\u53d1\u4e00\u4e2a\u9700\u8981\u4e2d\u6587\u754c\u9762\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u652f\u6301\u4e2d\u6587\u7684GUI\u5e93\uff0c\u5982Tkinter\u3001PyQt\u7b49\u3002\u8fd9\u4e9b\u5e93\u901a\u5e38\u652f\u6301\u591a\u8bed\u8a00\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8bbe\u7f6e\u754c\u9762\u8bed\u8a00\u4e3a\u4e2d\u6587\u3002<\/p>\n<\/p>\n<ol>\n<li>Tkinter\u4e2d\u6587\u754c\u9762<\/li>\n<\/ol>\n<p><p>Tkinter\u662fPython\u5185\u7f6e\u7684GUI\u5e93\uff0c\u652f\u6301\u591a\u8bed\u8a00\u663e\u793a\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u5b57\u4f53\u7b49\u65b9\u5f0f\u652f\u6301\u4e2d\u6587\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>PyQt\u4e2d\u6587\u754c\u9762<\/li>\n<\/ol>\n<p><p>PyQt\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684GUI\u5e93\uff0c\u652f\u6301\u591a\u8bed\u8a00\uff0c\u53ef\u4ee5\u901a\u8fc7Qt Designer\u8bbe\u8ba1\u754c\u9762\uff0c\u5e76\u5728\u4ee3\u7801\u4e2d\u8bbe\u7f6e\u8bed\u8a00\u4e3a\u4e2d\u6587\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u5904\u7406\u4e2d\u6587\u6587\u672c\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e2d\u6587\u6587\u672c\u6570\u636e\u65f6\uff0c\u9700\u8981\u8003\u8651\u5206\u8bcd\u3001\u62fc\u97f3\u8f6c\u6362\u3001\u7e41\u7b80\u8f6c\u6362\u7b49\u95ee\u9898\u3002\u4f7f\u7528\u524d\u9762\u63d0\u5230\u7684\u4e2d\u6587\u5e93\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u5904\u7406\u8fd9\u4e9b\u95ee\u9898\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4e2d\u6587\u5206\u8bcd<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528jieba\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u4e2d\u6587\u6587\u672c\u8fdb\u884c\u5206\u8bcd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import jieba<\/p>\n<p>\u6587\u672c = &quot;\u6211\u7231\u5b66\u4e60Python&quot;<\/p>\n<p>\u5206\u8bcd\u7ed3\u679c = jieba.lcut(\u6587\u672c)<\/p>\n<p>print(&quot;\u5206\u8bcd\u7ed3\u679c\uff1a&quot;, \u5206\u8bcd\u7ed3\u679c)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u6c49\u5b57\u8f6c\u62fc\u97f3<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528pypinyin\u5e93\u53ef\u4ee5\u5c06\u6c49\u5b57\u8f6c\u6362\u4e3a\u62fc\u97f3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pypinyin import pinyin, lazy_pinyin<\/p>\n<p>\u6c49\u5b57 = &quot;\u4f60\u597d&quot;<\/p>\n<p>\u62fc\u97f3 = lazy_pinyin(\u6c49\u5b57)<\/p>\n<p>print(&quot;\u62fc\u97f3\uff1a&quot;, \u62fc\u97f3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u8ba9Python\u7a0b\u5e8f\u66f4\u597d\u5730\u9002\u5e94\u4e2d\u6587\u73af\u5883\uff0c\u65e0\u8bba\u662f\u754c\u9762\u3001\u8f93\u5165\u8f93\u51fa\u8fd8\u662f\u6587\u672c\u5904\u7406\u90fd\u80fd\u591f\u6d41\u7545\u5730\u4f7f\u7528\u4e2d\u6587\u3002\u5e0c\u671b\u8fd9\u4e9b\u65b9\u6cd5\u80fd\u591f\u5e2e\u52a9\u4f60\u5728Python\u5f00\u53d1\u4e2d\u66f4\u597d\u5730\u8fd0\u7528\u4e2d\u6587\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bbe\u7f6e\u4e2d\u6587\u7f16\u7801\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5982\u679c\u5e0c\u671b\u5904\u7406\u4e2d\u6587\u6587\u672c\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u6e90\u4ee3\u7801\u6587\u4ef6\u4f7f\u7528UTF-8\u7f16\u7801\u3002\u53ef\u4ee5\u5728\u6587\u4ef6\u7684\u5f00\u5934\u6dfb\u52a0<code># -*- coding: utf-8 -*-<\/code>\u6765\u6307\u5b9a\u7f16\u7801\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>print<\/code>\u8bed\u53e5\u65f6\uff0c\u786e\u4fdd\u5b57\u7b26\u4e32\u662f\u4ee5\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\u5b58\u50a8\u3002\u5bf9\u4e8ePython 3\uff0c\u901a\u5e38\u5b57\u7b26\u4e32\u9ed8\u8ba4\u662fUnicode\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8bfb\u53d6\u4e2d\u6587\u6587\u4ef6\uff1f<\/strong><br \/>\u8bfb\u53d6\u4e2d\u6587\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\uff0c\u5e76\u6307\u5b9a\u7f16\u7801\u4e3aUTF-8\u3002\u4f8b\u5982\uff0c<code>with open(&#39;\u6587\u4ef6\u540d.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as f:<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u4e2d\u7684\u4e2d\u6587\u5185\u5bb9\u80fd\u591f\u6b63\u786e\u8bfb\u53d6\uff0c\u907f\u514d\u4e71\u7801\u95ee\u9898\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u7a0b\u5e8f\u4e2d\u8f93\u51fa\u4e2d\u6587\u5b57\u7b26\uff1f<\/strong><br \/>\u8981\u5728Python\u7a0b\u5e8f\u4e2d\u8f93\u51fa\u4e2d\u6587\u5b57\u7b26\uff0c\u786e\u4fdd\u4f7f\u7528\u7684\u73af\u5883\u652f\u6301UTF-8\u7f16\u7801\u3002\u4f8b\u5982\uff0c\u5728\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\u4e2d\u8fd0\u884c\u7a0b\u5e8f\u65f6\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u7ec8\u7aef\u7f16\u7801\u4e3aUTF-8\u3002\u4f7f\u7528<code>print(&quot;\u4f60\u597d\uff0c\u4e16\u754c&quot;)<\/code>\u8fd9\u6837\u7684\u8bed\u53e5\u53ef\u4ee5\u76f4\u63a5\u8f93\u51fa\u4e2d\u6587\u5b57\u7b26\u3002\u5982\u679c\u5728\u67d0\u4e9bIDE\u4e2d\u9047\u5230\u4e71\u7801\uff0c\u53ef\u4ee5\u68c0\u67e5IDE\u7684\u7f16\u7801\u8bbe\u7f6e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5c06Python\u6539\u6210\u4e2d\u6587\u5f62\u5f0f\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b89\u88c5\u4e2d\u6587\u5e93\u3001\u8bbe\u7f6e\u4e2d\u6587\u7f16\u7801\u3001\u4f7f\u7528\u4e2d\u6587\u6ce8\u91ca\u4e0e\u53d8\u91cf\u540d\u3001\u4f7f\u7528\u4e2d\u6587\u6587\u6863\u7b49\u65b9\u5f0f\u6765\u5b9e\u73b0 [&hellip;]","protected":false},"author":3,"featured_media":994753,"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\/994745"}],"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=994745"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/994745\/revisions"}],"predecessor-version":[{"id":994754,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/994745\/revisions\/994754"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/994753"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=994745"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=994745"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=994745"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}