{"id":1019992,"date":"2024-12-27T13:06:53","date_gmt":"2024-12-27T05:06:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1019992.html"},"modified":"2024-12-27T13:06:55","modified_gmt":"2024-12-27T05:06:55","slug":"python%e5%a6%82%e4%bd%95%e6%94%b9%e4%b8%ba%e8%8b%b1%e6%96%87%e7%9a%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1019992.html","title":{"rendered":"python\u5982\u4f55\u6539\u4e3a\u82f1\u6587\u7684"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25162422\/2e9c5003-fedb-41aa-8e91-beb9091770fa.webp\" alt=\"python\u5982\u4f55\u6539\u4e3a\u82f1\u6587\u7684\" \/><\/p>\n<p><p> \u8981\u5c06Python\u7684\u754c\u9762\u8bed\u8a00\u6216\u8f93\u51fa\u5185\u5bb9\u6539\u4e3a\u82f1\u6587\uff0c<strong>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5<\/strong>\uff1a\u66f4\u6539Python\u89e3\u91ca\u5668\u7684\u9ed8\u8ba4\u8bed\u8a00\u3001\u66f4\u6539IDE\uff08\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff09\u7684\u8bed\u8a00\u8bbe\u7f6e\u3001\u6216\u901a\u8fc7\u4ee3\u7801\u4e2d\u6307\u5b9a\u8bed\u8a00\u3002\u4f8b\u5982\uff0c<strong>\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u73af\u5883\u53d8\u91cf\u6765\u8bbe\u7f6e\u8bed\u8a00<\/strong>\u3002\u5728Windows\u4e0a\uff0c\u53ef\u4ee5\u901a\u8fc7\u66f4\u6539\u7cfb\u7edf\u7684\u533a\u57df\u8bbe\u7f6e\u6765\u5b9e\u73b0\u3002\u6216\u8005\uff0c\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528<code>locale<\/code>\u5e93\u6765\u6307\u5b9a\u8f93\u51fa\u8bed\u8a00\uff0c\u4f8b\u5982\u4f7f\u7528<code>locale.setlocale(locale.LC_ALL, &#39;en_US&#39;)<\/code>\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4fee\u6539\u7cfb\u7edf\u73af\u5883\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u64cd\u4f5c\u7cfb\u7edf\u4e0a\uff0cPython\u4f1a\u6839\u636e\u7cfb\u7edf\u7684\u8bed\u8a00\u8bbe\u7f6e\u6765\u51b3\u5b9a\u9ed8\u8ba4\u7684\u8bed\u8a00\u8f93\u51fa\u3002\u56e0\u6b64\uff0c\u4fee\u6539\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf\u53ef\u80fd\u4f1a\u5f71\u54cdPython\u7684\u8bed\u8a00\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<p><h4>1.1 Windows\u7cfb\u7edf<\/h4>\n<\/p>\n<p><p>\u5728Windows\u7cfb\u7edf\u4e0a\uff0c\u53ef\u4ee5\u901a\u8fc7\u63a7\u5236\u9762\u677f\u4fee\u6539\u533a\u57df\u548c\u8bed\u8a00\u8bbe\u7f6e\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u201c\u63a7\u5236\u9762\u677f\u201d\u3002<\/li>\n<li>\u9009\u62e9\u201c\u65f6\u949f\u548c\u533a\u57df\u201d\u3002<\/li>\n<li>\u70b9\u51fb\u201c\u533a\u57df\u201d\u3002<\/li>\n<li>\u5728\u201c\u683c\u5f0f\u201d\u9009\u9879\u5361\u4e2d\uff0c\u9009\u62e9\u201c\u82f1\u8bed\uff08\u7f8e\u56fd\uff09\u201d\u6216\u5176\u4ed6\u82f1\u8bed\u9009\u9879\u3002<\/li>\n<li>\u786e\u8ba4\u5e76\u91cd\u542f\u8ba1\u7b97\u673a\u4ee5\u5e94\u7528\u66f4\u6539\u3002<\/li>\n<\/ol>\n<p><h4>1.2 macOS\u7cfb\u7edf<\/h4>\n<\/p>\n<p><p>\u5728macOS\u4e0a\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u7cfb\u7edf\u504f\u597d\u8bbe\u7f6e\u6765\u66f4\u6539\u8bed\u8a00\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u201c\u7cfb\u7edf\u504f\u597d\u8bbe\u7f6e\u201d\u3002<\/li>\n<li>\u70b9\u51fb\u201c\u8bed\u8a00\u4e0e\u5730\u533a\u201d\u3002<\/li>\n<li>\u5728\u201c\u9996\u9009\u8bed\u8a00\u201d\u4e2d\u6dfb\u52a0\u201c\u82f1\u8bed\u201d\u5e76\u5c06\u5176\u62d6\u52a8\u5230\u9876\u90e8\u3002<\/li>\n<li>\u91cd\u65b0\u542f\u52a8\u8ba1\u7b97\u673a\u4ee5\u5e94\u7528\u66f4\u6539\u3002<\/li>\n<\/ol>\n<p><h4>1.3 Linux\u7cfb\u7edf<\/h4>\n<\/p>\n<p><p>\u5728Linux\u7cfb\u7edf\u4e0a\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u66f4\u6539\u8bed\u8a00\u8bbe\u7f6e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">export LANG=en_US.UTF-8<\/p>\n<p>export LANGUAGE=en_US.UTF-8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u547d\u4ee4\u4f1a\u66f4\u6539\u7ec8\u7aef\u4f1a\u8bdd\u7684\u8bed\u8a00\u8bbe\u7f6e\uff0c\u5e76\u53ef\u80fd\u5f71\u54cdPython\u7684\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u8bbe\u7f6eIDE\u8bed\u8a00<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u60a8\u4f7f\u7528\u7684\u662f\u4e00\u4e2a\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u6765\u7f16\u5199Python\u4ee3\u7801\uff0c\u90a3\u4e48\u66f4\u6539IDE\u7684\u8bed\u8a00\u8bbe\u7f6e\u53ef\u80fd\u4f1a\u5f71\u54cdPython\u7684\u8bed\u8a00\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h4>2.1 PyCharm<\/h4>\n<\/p>\n<p><p>\u5728PyCharm\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u66f4\u6539\u8bed\u8a00\u8bbe\u7f6e\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00PyCharm\u3002<\/li>\n<li>\u8fdb\u5165\u201cFile\u201d -&gt; \u201cSettings\u201d\u3002<\/li>\n<li>\u5728\u201cAppearance &amp; Behavior\u201d\u4e0b\u9009\u62e9\u201cLanguages &amp; Frameworks\u201d\u3002<\/li>\n<li>\u5c06\u8bed\u8a00\u8bbe\u7f6e\u4e3a\u201cEnglish\u201d\u3002<\/li>\n<\/ol>\n<p><h4>2.2 VS Code<\/h4>\n<\/p>\n<p><p>\u5728Visual Studio Code\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u6269\u5c55\u6765\u8bbe\u7f6e\u8bed\u8a00\u5305\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00VS Code\u3002<\/li>\n<li>\u8fdb\u5165\u201cView\u201d -&gt; \u201cExtensions\u201d\u3002<\/li>\n<li>\u641c\u7d22\u5e76\u5b89\u88c5\u201cEnglish Language Pack\u201d\u3002<\/li>\n<li>\u91cd\u542fVS Code\u4ee5\u5e94\u7528\u66f4\u6539\u3002<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>locale<\/code>\u5e93<\/h3>\n<\/p>\n<p><p>\u5728Python\u4ee3\u7801\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>locale<\/code>\u5e93\u6765\u8bbe\u7f6e\u7a0b\u5e8f\u7684\u8bed\u8a00\u73af\u5883\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u52a8\u6001\u66f4\u6539\u8bed\u8a00\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>3.1 <code>locale<\/code>\u6a21\u5757\u4ecb\u7ecd<\/h4>\n<\/p>\n<p><p><code>locale<\/code>\u6a21\u5757\u4e3a\u7a0b\u5e8f\u63d0\u4f9b\u4e86\u56fd\u9645\u5316\u548c\u672c\u5730\u5316\u7684\u652f\u6301\u3002\u901a\u8fc7\u8bbe\u7f6e\u5408\u9002\u7684\u8bed\u8a00\u73af\u5883\uff0c\u60a8\u53ef\u4ee5\u63a7\u5236\u5b57\u7b26\u4e32\u3001\u65e5\u671f\u3001\u65f6\u95f4\u548c\u8d27\u5e01\u7684\u663e\u793a\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>3.2 \u4f7f\u7528<code>locale.setlocale()<\/code><\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>locale.setlocale()<\/code>\u51fd\u6570\u6765\u8bbe\u7f6e\u8bed\u8a00\u73af\u5883\u3002\u4f8b\u5982\uff0c\u8bbe\u7f6e\u4e3a\u82f1\u8bed\uff08\u7f8e\u56fd\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import locale<\/p>\n<p>locale.setlocale(locale.LC_ALL, &#39;en_US.UTF-8&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.3 \u83b7\u53d6\u5f53\u524d\u8bed\u8a00\u8bbe\u7f6e<\/h4>\n<\/p>\n<p><p>\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>locale.getlocale()<\/code>\u6765\u83b7\u53d6\u5f53\u524d\u7684\u8bed\u8a00\u8bbe\u7f6e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">current_locale = locale.getlocale()<\/p>\n<p>print(&quot;Current Locale:&quot;, current_locale)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u8fdb\u884c\u7ffb\u8bd1<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5c06\u7a0b\u5e8f\u8f93\u51fa\u7684\u6587\u672c\u7ffb\u8bd1\u4e3a\u82f1\u6587\uff0c\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u7ffb\u8bd1\u5e93\uff0c\u5982<code>googletrans<\/code>\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u5b89\u88c5<code>googletrans<\/code><\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5<code>googletrans<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install googletrans==4.0.0-rc1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2 \u4f7f\u7528<code>googletrans<\/code>\u8fdb\u884c\u7ffb\u8bd1<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5c06\u6587\u672c\u7ffb\u8bd1\u4e3a\u82f1\u6587\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from googletrans import Translator<\/p>\n<p>translator = Translator()<\/p>\n<p>translated = translator.translate(&#39;\u4f60\u597d\uff0c\u4e16\u754c&#39;, dest=&#39;en&#39;)<\/p>\n<p>print(translated.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4ee3\u7801\u7247\u6bb5\u4f1a\u5c06\u4e2d\u6587\u201c\u4f60\u597d\uff0c\u4e16\u754c\u201d\u7ffb\u8bd1\u4e3a\u82f1\u6587\u201cHello, World\u201d\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5c06Python\u7684\u8bed\u8a00\u8bbe\u7f6e\u4e3a\u82f1\u6587\u3002<strong>\u5177\u4f53\u65b9\u6cd5\u7684\u9009\u62e9\u4e3b\u8981\u53d6\u51b3\u4e8e\u60a8\u7684\u5177\u4f53\u9700\u6c42\u548c\u4f7f\u7528\u573a\u666f<\/strong>\u3002\u5982\u679c\u60a8\u5728\u5168\u7403\u5316\u9879\u76ee\u4e2d\u5de5\u4f5c\uff0c\u7406\u89e3\u548c\u4f7f\u7528\u4e0d\u540c\u8bed\u8a00\u8bbe\u7f6e\u7684\u80fd\u529b\u5c06\u5927\u5927\u63d0\u9ad8\u60a8\u7684\u9879\u76ee\u7684\u56fd\u9645\u5316\u6c34\u5e73\u3002\u5e0c\u671b\u4ee5\u4e0a\u5185\u5bb9\u80fd\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u7684\u8bed\u8a00\u8bbe\u7f6e\u4e3a\u82f1\u6587\uff1f<\/strong><br \/>\u8981\u5c06Python\u7684\u8bed\u8a00\u8bbe\u7f6e\u4e3a\u82f1\u6587\uff0c\u60a8\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u660e\u786e\u6307\u5b9a\u8bed\u8a00\u73af\u5883\u3002\u5177\u4f53\u65b9\u6cd5\u662f\u5728\u811a\u672c\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import locale\nlocale.setlocale(locale.LC_ALL, &#39;en_US.UTF-8&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u60a8\u7684\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u4f7f\u7528\u82f1\u6587\u7684\u533a\u57df\u8bbe\u7f6e\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u591a\u8bed\u8a00\u652f\u6301\uff1f<\/strong><br \/>\u5728Python\u4e2d\u5904\u7406\u591a\u8bed\u8a00\u652f\u6301\u901a\u5e38\u4f7f\u7528\u56fd\u9645\u5316\uff08i18n\uff09\u548c\u672c\u5730\u5316\uff08l10n\uff09\u6280\u672f\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>gettext<\/code>\u6a21\u5757\u6765\u7ba1\u7406\u4e0d\u540c\u8bed\u8a00\u7684\u7ffb\u8bd1\u3002\u901a\u8fc7\u521b\u5efa.po\u6587\u4ef6\uff0c\u60a8\u53ef\u4ee5\u5b9a\u4e49\u4e0d\u540c\u8bed\u8a00\u7684\u6587\u672c\u5185\u5bb9\uff0c\u5e76\u901a\u8fc7\u7a0b\u5e8f\u52a0\u8f7d\u76f8\u5e94\u7684\u7ffb\u8bd1\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python IDE\u4e2d\u66f4\u6539\u8bed\u8a00\u8bbe\u7f6e\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u4f7f\u7528\u7684\u662f\u67d0\u4e2a\u7279\u5b9a\u7684IDE\uff0c\u5982PyCharm\u6216VS Code\uff0c\u60a8\u53ef\u4ee5\u5728\u8bbe\u7f6e\u6216\u504f\u597d\u9009\u9879\u4e2d\u67e5\u627e\u8bed\u8a00\u6216\u533a\u57df\u8bbe\u7f6e\u7684\u9009\u9879\u3002\u901a\u5e38\uff0c\u60a8\u53ef\u4ee5\u5728\u201c\u8bbe\u7f6e\u201d\u83dc\u5355\u4e2d\u627e\u5230\u201c\u754c\u9762\u8bed\u8a00\u201d\u6216\u201c\u8bed\u8a00\u201d\u9009\u9879\uff0c\u5e76\u9009\u62e9\u201c\u82f1\u8bed\u201d\u4f5c\u4e3a\u9ed8\u8ba4\u8bed\u8a00\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fddIDE\u7684\u754c\u9762\u548c\u63d0\u793a\u4fe1\u606f\u663e\u793a\u4e3a\u82f1\u6587\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5c06Python\u7684\u754c\u9762\u8bed\u8a00\u6216\u8f93\u51fa\u5185\u5bb9\u6539\u4e3a\u82f1\u6587\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u66f4\u6539Python\u89e3\u91ca\u5668\u7684\u9ed8\u8ba4\u8bed\u8a00\u3001\u66f4\u6539ID [&hellip;]","protected":false},"author":3,"featured_media":1019996,"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\/1019992"}],"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=1019992"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1019992\/revisions"}],"predecessor-version":[{"id":1019997,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1019992\/revisions\/1019997"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1019996"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1019992"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1019992"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1019992"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}