{"id":923109,"date":"2024-12-26T14:44:03","date_gmt":"2024-12-26T06:44:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/923109.html"},"modified":"2024-12-26T14:44:05","modified_gmt":"2024-12-26T06:44:05","slug":"python%e5%a6%82%e4%bd%95%e6%b1%89%e5%8c%96","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/923109.html","title":{"rendered":"python\u5982\u4f55\u6c49\u5316"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211327\/d61012df-876c-4949-8f2a-1f398773e64d.webp\" alt=\"python\u5982\u4f55\u6c49\u5316\" \/><\/p>\n<p><p> <strong>Python\u6c49\u5316\u4e3b\u8981\u901a\u8fc7\u4fee\u6539\u8f6f\u4ef6\u754c\u9762\u7684\u8bed\u8a00\u6587\u4ef6\u3001\u4f7f\u7528\u7ffb\u8bd1\u5e93\u3001\u8bbe\u7f6e\u7f16\u7801\u652f\u6301\u7b49\u65b9\u5f0f\u5b9e\u73b0\u3002<\/strong>\u5176\u4e2d\uff0c\u4fee\u6539\u8f6f\u4ef6\u754c\u9762\u7684\u8bed\u8a00\u6587\u4ef6\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u901a\u5e38\u9700\u8981\u5c06\u5e94\u7528\u7a0b\u5e8f\u7684\u754c\u9762\u6587\u672c\u5b57\u7b26\u4e32\u66ff\u6362\u4e3a\u4e2d\u6587\u3002\u5177\u4f53\u5b9e\u73b0\u5305\u62ec\u521b\u5efa\u6216\u4fee\u6539\u8bed\u8a00\u6587\u4ef6\uff08\u5982gettext\u7684.po\u6587\u4ef6\uff09\uff0c\u7136\u540e\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u52a0\u8f7d\u76f8\u5e94\u7684\u8bed\u8a00\u6587\u4ef6\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5229\u7528\u8fd9\u4e9b\u65b9\u6cd5\u5bf9Python\u5e94\u7528\u7a0b\u5e8f\u8fdb\u884c\u6c49\u5316\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4fee\u6539\u8f6f\u4ef6\u754c\u9762\u7684\u8bed\u8a00\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u5728Python\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u754c\u9762\u6587\u672c\u901a\u5e38\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u51fa\u73b0\u3002\u4e3a\u4e86\u5b9e\u73b0\u6c49\u5316\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u548c\u4fee\u6539\u8bed\u8a00\u6587\u4ef6\u6765\u66ff\u6362\u8fd9\u4e9b\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528gettext\u8fdb\u884c\u56fd\u9645\u5316<\/strong><\/p>\n<\/p>\n<ul>\n<li>gettext\u662fPython\u4e2d\u5e38\u7528\u7684\u56fd\u9645\u5316\u5de5\u5177\u3002\u901a\u8fc7gettext\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u4e3a\u5e94\u7528\u7a0b\u5e8f\u6dfb\u52a0\u591a\u8bed\u8a00\u652f\u6301\u3002<\/li>\n<li>\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u5b89\u88c5gettext\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5\uff1a\n<pre><code>pip install gettext<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u5728\u6e90\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528<code>_()<\/code>\u51fd\u6570\u5305\u88f9\u9700\u8981\u7ffb\u8bd1\u7684\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a\n<pre><code class=\"language-python\">print(_(&quot;Hello, World!&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u7136\u540e\uff0c\u751f\u6210pot\u6587\u4ef6\uff08\u7ffb\u8bd1\u6a21\u677f\uff09\uff0c\u5e76\u6839\u636e\u8bed\u8a00\u751f\u6210po\u6587\u4ef6\uff08\u7ffb\u8bd1\u6587\u4ef6\uff09\u3002\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u751f\u6210pot\u6587\u4ef6\uff1a\n<pre><code>pygettext -o messages.pot your_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u5c06\u751f\u6210\u7684messages.pot\u590d\u5236\u4e3azh_CN.po\uff0c\u5e76\u5728\u5176\u4e2d\u6dfb\u52a0\u4e2d\u6587\u7ffb\u8bd1\u3002<\/li>\n<li>\u7f16\u8bd1po\u6587\u4ef6\u4e3amo\u6587\u4ef6\uff1a\n<pre><code>msgfmt zh_CN.po -o zh_CN.mo<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u52a0\u8f7d\u5bf9\u5e94\u7684mo\u6587\u4ef6\uff1a\n<pre><code class=\"language-python\">import gettext<\/p>\n<p>lang = gettext.translation(&#39;base&#39;, localedir=&#39;locales&#39;, languages=[&#39;zh_CN&#39;])<\/p>\n<p>lang.install()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u624b\u52a8\u66ff\u6362\u5b57\u7b26\u4e32<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5bf9\u4e8e\u8f83\u5c0f\u7684\u9879\u76ee\uff0c\u53ef\u4ee5\u624b\u52a8\u66ff\u6362\u4ee3\u7801\u4e2d\u7684\u82f1\u6587\u5b57\u7b26\u4e32\u4e3a\u4e2d\u6587\u3002<\/li>\n<li>\u786e\u4fdd\u6240\u6709\u66ff\u6362\u7684\u5b57\u7b26\u4e32\u5728\u4ee3\u7801\u4e2d\u4e00\u81f4\uff0c\u5e76\u4e14\u7f16\u7801\u8bbe\u7f6e\u4e3aUTF-8\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u7ffb\u8bd1\u5e93<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>googletrans\u5e93<\/strong><\/p>\n<\/p>\n<ul>\n<li>googletrans\u662f\u4e00\u4e2aPython\u5e93\uff0c\u53ef\u4ee5\u8c03\u7528Google\u7ffb\u8bd1API\u5b9e\u73b0\u6587\u672c\u7ffb\u8bd1\u3002\u4f7f\u7528\u8fd9\u4e2a\u5e93\u53ef\u4ee5\u5feb\u901f\u5c06\u5e94\u7528\u7a0b\u5e8f\u7684\u6587\u672c\u7ffb\u8bd1\u6210\u4e2d\u6587\u3002<\/li>\n<li>\u5b89\u88c5googletrans\uff1a\n<pre><code>pip install googletrans==4.0.0-rc1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u4f7f\u7528googletrans\u8fdb\u884c\u7ffb\u8bd1\uff1a\n<pre><code class=\"language-python\">from googletrans import Translator<\/p>\n<p>translator = Translator()<\/p>\n<p>result = translator.translate(&#39;Hello, world!&#39;, dest=&#39;zh-cn&#39;)<\/p>\n<p>print(result.text)  # \u8f93\u51fa\uff1a\u4f60\u597d\uff0c\u4e16\u754c\uff01<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5176\u4ed6\u7ffb\u8bd1API<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5176\u4ed6\u7ffb\u8bd1API\uff0c\u5982\u5fae\u8f6fAzure\u7ffb\u8bd1API\u3001\u767e\u5ea6\u7ffb\u8bd1API\u7b49\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8ePython\u7a0b\u5e8f\u7684\u6c49\u5316\u3002<\/li>\n<li>\u4f7f\u7528\u8fd9\u4e9bAPI\u901a\u5e38\u9700\u8981\u6ce8\u518c\u5f00\u53d1\u8005\u8d26\u53f7\uff0c\u5e76\u83b7\u53d6API\u5bc6\u94a5\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u8bbe\u7f6e\u7f16\u7801\u652f\u6301<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8bbe\u7f6e\u7f16\u7801\u4e3aUTF-8<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u786e\u4fddPython\u7a0b\u5e8f\u7684\u6e90\u6587\u4ef6\u7f16\u7801\u4e3aUTF-8\uff0c\u4ee5\u652f\u6301\u4e2d\u6587\u5b57\u7b26\u663e\u793a\u3002<\/li>\n<li>\u5728Python\u6587\u4ef6\u7684\u9876\u90e8\u6dfb\u52a0\u7f16\u7801\u58f0\u660e\uff1a\n<pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u4e2d\u6587\u8f93\u5165\u8f93\u51fa<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5728\u5904\u7406\u4e2d\u6587\u8f93\u5165\u8f93\u51fa\u65f6\uff0c\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u3002\u5bf9\u4e8ePython 3\uff0c\u9ed8\u8ba4\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u56e0\u6b64\u65e0\u9700\u989d\u5916\u8bbe\u7f6e\u3002<\/li>\n<li>\u5982\u679c\u4f7f\u7528Python 2\uff0c\u9700\u663e\u5f0f\u8bbe\u7f6e\u7f16\u7801\uff1a\n<pre><code class=\"language-python\">import sys<\/p>\n<p>reload(sys)<\/p>\n<p>sys.setdefaultencoding(&#39;utf-8&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u8fdb\u884cGUI\u6c49\u5316<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>PyQt\/PySide<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5bf9\u4e8e\u4f7f\u7528PyQt\u6216PySide\u521b\u5efa\u7684GUI\u5e94\u7528\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u901a\u8fc7Qt\u7684\u7ffb\u8bd1\u673a\u5236\u8fdb\u884c\u6c49\u5316\u3002<\/li>\n<li>\u4f7f\u7528Qt Linguist\u5de5\u5177\u7f16\u8f91\u7ffb\u8bd1\u6587\u4ef6\uff08.ts\uff09\uff0c\u5e76\u751f\u6210\u4e8c\u8fdb\u5236\u7ffb\u8bd1\u6587\u4ef6\uff08.qm\uff09\u3002<\/li>\n<li>\u5728\u7a0b\u5e8f\u4e2d\u52a0\u8f7d\u7ffb\u8bd1\u6587\u4ef6\uff1a\n<pre><code class=\"language-python\">from PyQt5.QtCore import QTranslator<\/p>\n<p>translator = QTranslator()<\/p>\n<p>translator.load(&#39;zh_CN.qm&#39;)<\/p>\n<p>app.installTranslator(translator)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>Tkinter<\/strong><\/p>\n<\/p>\n<ul>\n<li>Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\u3002\u5bf9\u4e8eTkinter\uff0c\u901a\u5e38\u9700\u8981\u624b\u52a8\u66ff\u6362\u754c\u9762\u4e2d\u7684\u6587\u672c\u3002<\/li>\n<li>\u4e5f\u53ef\u4ee5\u7ed3\u5408gettext\u8fdb\u884c\u591a\u8bed\u8a00\u652f\u6301\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9009\u62e9\u5408\u9002\u7684\u7ffb\u8bd1\u65b9\u6848<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u6839\u636e\u9879\u76ee\u89c4\u6a21\u548c\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u7ffb\u8bd1\u65b9\u6848\u3002\u5bf9\u4e8e\u5c0f\u578b\u9879\u76ee\uff0c\u624b\u52a8\u66ff\u6362\u53ef\u80fd\u66f4\u5feb\u6377\uff1b\u5bf9\u4e8e\u5927\u578b\u9879\u76ee\uff0c\u4f7f\u7528gettext\u6216\u5176\u4ed6\u7ffb\u8bd1\u5de5\u5177\u66f4\u4e3a\u9ad8\u6548\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u7ffb\u8bd1\u8d28\u91cf<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u786e\u4fdd\u7ffb\u8bd1\u7684\u51c6\u786e\u6027\u548c\u4e00\u81f4\u6027\u3002\u5bf9\u4e8e\u81ea\u52a8\u7ffb\u8bd1\u7ed3\u679c\uff0c\u5efa\u8bae\u8fdb\u884c\u4eba\u5de5\u6821\u5bf9\u548c\u8c03\u6574\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6d4b\u8bd5\u548c\u9a8c\u8bc1<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5728\u6c49\u5316\u5b8c\u6210\u540e\uff0c\u8fdb\u884c\u5168\u9762\u7684\u6d4b\u8bd5\u548c\u9a8c\u8bc1\uff0c\u786e\u4fdd\u6240\u6709\u754c\u9762\u548c\u529f\u80fd\u6b63\u5e38\u5de5\u4f5c\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6709\u6548\u5730\u4e3aPython\u5e94\u7528\u7a0b\u5e8f\u6dfb\u52a0\u4e2d\u6587\u652f\u6301\uff0c\u5b9e\u73b0\u6c49\u5316\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u9879\u76ee\u7684\u5177\u4f53\u9700\u6c42\u548c\u7279\u70b9\uff0c\u7075\u6d3b\u9009\u62e9\u548c\u7ec4\u5408\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5c06\u6709\u52a9\u4e8e\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u4e2d\u6587\u663e\u793a\uff1f<\/strong><br \/>\u4e3a\u4e86\u5728Python\u4e2d\u5b9e\u73b0\u4e2d\u6587\u663e\u793a\uff0c\u9700\u8981\u786e\u4fdd\u4f60\u7684\u4ee3\u7801\u6587\u4ef6\u4fdd\u5b58\u4e3aUTF-8\u7f16\u7801\u3002\u6b64\u5916\uff0c\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528<code>print<\/code>\u51fd\u6570\u65f6\uff0c\u786e\u4fdd\u5b57\u7b26\u4e32\u662f\u4e2d\u6587\u5b57\u7b26\uff0c\u5e76\u4e14\u8fd0\u884c\u73af\u5883\u652f\u6301\u4e2d\u6587\u663e\u793a\u3002\u53ef\u4ee5\u4f7f\u7528<code>print(&quot;\u4f60\u597d\uff0c\u4e16\u754c\uff01&quot;)<\/code>\u6765\u6d4b\u8bd5\u4e2d\u6587\u8f93\u51fa\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u7f16\u7801\uff1f<\/strong><br \/>\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u7f16\u7801\u65f6\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u786e\u4fdd\u5728\u6253\u5f00\u6587\u4ef6\u65f6\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\u3002\u4f7f\u7528<code>open(&#39;\u6587\u4ef6\u540d&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;)<\/code>\u53ef\u4ee5\u907f\u514d\u56e0\u7f16\u7801\u4e0d\u4e00\u81f4\u5bfc\u81f4\u7684\u9519\u8bef\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>str.encode(&#39;utf-8&#39;)<\/code>\u548c<code>bytes.decode(&#39;utf-8&#39;)<\/code>\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u4e0e\u5b57\u8282\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<p><strong>\u600e\u6837\u5728Python\u4e2d\u4f7f\u7528\u4e2d\u6587\u5e93\u548c\u6a21\u5757\uff1f<\/strong><br \/>\u5728Python\u4e2d\u4f7f\u7528\u4e2d\u6587\u5e93\u548c\u6a21\u5757\u65f6\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u76f8\u5173\u7684\u7b2c\u4e09\u65b9\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528<code>pip install<\/code>\u547d\u4ee4\u5b89\u88c5\u5e93\uff0c\u5982<code>pip install pypinyin<\/code>\u3002\u5b89\u88c5\u540e\uff0c\u786e\u4fdd\u5728\u4ee3\u7801\u4e2d\u6b63\u786e\u5f15\u7528\u8fd9\u4e9b\u5e93\uff0c\u5e76\u6ce8\u610f\u6587\u6863\u4e2d\u7684\u4e2d\u6587\u652f\u6301\u8bf4\u660e\uff0c\u4ee5\u4fbf\u987a\u5229\u4f7f\u7528\u5176\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u6c49\u5316\u4e3b\u8981\u901a\u8fc7\u4fee\u6539\u8f6f\u4ef6\u754c\u9762\u7684\u8bed\u8a00\u6587\u4ef6\u3001\u4f7f\u7528\u7ffb\u8bd1\u5e93\u3001\u8bbe\u7f6e\u7f16\u7801\u652f\u6301\u7b49\u65b9\u5f0f\u5b9e\u73b0\u3002\u5176\u4e2d\uff0c\u4fee\u6539\u8f6f\u4ef6\u754c\u9762\u7684\u8bed\u8a00\u6587 [&hellip;]","protected":false},"author":3,"featured_media":923114,"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\/923109"}],"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=923109"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/923109\/revisions"}],"predecessor-version":[{"id":923115,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/923109\/revisions\/923115"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/923114"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=923109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=923109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=923109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}