{"id":1024383,"date":"2024-12-30T14:13:24","date_gmt":"2024-12-30T06:13:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1024383.html"},"modified":"2024-12-30T14:13:25","modified_gmt":"2024-12-30T06:13:25","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%bf%bb%e8%af%91id-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1024383.html","title":{"rendered":"\u5982\u4f55\u7528python\u7ffb\u8bd1ID"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/349a169a-e64b-4f89-b3e8-e0c5578ee097.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u7528python\u7ffb\u8bd1ID\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u7528Python\u7ffb\u8bd1ID<\/strong><\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528Python\u7ffb\u8bd1ID\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5982\u8c03\u7528API\u3001\u4f7f\u7528\u5f00\u6e90\u5e93\u3001\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u7b49\u3002<\/strong>\u5176\u4e2d\uff0c\u8c03\u7528API\u662f\u6700\u5e38\u7528\u4e14\u4fbf\u6377\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7API\u7ffb\u8bd1ID\uff0c\u53ef\u4ee5\u786e\u4fdd\u7ffb\u8bd1\u7684\u51c6\u786e\u6027\u548c\u4e00\u81f4\u6027\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u8c03\u7528API\u8fdb\u884cID\u7ffb\u8bd1\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u8c03\u7528API\u8fdb\u884cID\u7ffb\u8bd1<\/h3>\n<\/p>\n<p><p>\u8c03\u7528API\u8fdb\u884cID\u7ffb\u8bd1\u662f\u6700\u4e3a\u76f4\u63a5\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\u3002\u8bb8\u591a\u5728\u7ebf\u670d\u52a1\u63d0\u4f9b\u4e86\u7ffb\u8bd1API\uff0c\u4f8b\u5982Google\u7ffb\u8bd1API\u3001Microsoft Translator Text API\u7b49\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Google\u7ffb\u8bd1API\u8fdb\u884cID\u7ffb\u8bd1\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>1. \u83b7\u53d6API\u5bc6\u94a5<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5728Google Cloud Platform\uff08GCP\uff09\u4e0a\u521b\u5efa\u4e00\u4e2a\u9879\u76ee\u5e76\u542f\u7528Google Translate API\uff0c\u7136\u540e\u83b7\u53d6API\u5bc6\u94a5\u3002<\/p>\n<\/p>\n<p><h4>2. \u5b89\u88c5\u5fc5\u8981\u7684Python\u5e93<\/h4>\n<\/p>\n<p><p>\u4f60\u9700\u8981\u5b89\u88c5<code>googletrans<\/code>\u5e93\uff0c\u5b83\u662f\u4e00\u4e2a\u975e\u5b98\u65b9\u7684Google\u7ffb\u8bd1API\u5ba2\u6237\u7aef\u3002<\/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>3. \u7f16\u5199Python\u4ee3\u7801\u8fdb\u884c\u7ffb\u8bd1<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>googletrans<\/code>\u5e93\u8fdb\u884cID\u7ffb\u8bd1\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from googletrans import Translator<\/p>\n<p>def translate_id(text, src_lang=&#39;id&#39;, dest_lang=&#39;en&#39;):<\/p>\n<p>    translator = Translator()<\/p>\n<p>    translation = translator.translate(text, src=src_lang, dest=dest_lang)<\/p>\n<p>    return translation.text<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>id_text = &quot;Selamat datang di dunia Python&quot;<\/p>\n<p>translated_text = translate_id(id_text)<\/p>\n<p>print(translated_text)  # \u8f93\u51fa\uff1aWelcome to the world of Python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5f00\u6e90\u5e93\u8fdb\u884cID\u7ffb\u8bd1<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u8c03\u7528API\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u5f00\u6e90\u7684\u7ffb\u8bd1\u5e93\u8fdb\u884cID\u7ffb\u8bd1\u3002\u4f8b\u5982\uff0c<code>translate<\/code>\u5e93\u662f\u4e00\u4e2a\u7b80\u5355\u6613\u7528\u7684\u7ffb\u8bd1\u5e93\uff0c\u652f\u6301\u591a\u79cd\u8bed\u8a00\u7684\u7ffb\u8bd1\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5<code>translate<\/code>\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install translate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u7f16\u5199Python\u4ee3\u7801\u8fdb\u884c\u7ffb\u8bd1<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528<code>translate<\/code>\u5e93\u8fdb\u884cID\u7ffb\u8bd1\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from translate import Translator<\/p>\n<p>def translate_id(text, src_lang=&#39;id&#39;, dest_lang=&#39;en&#39;):<\/p>\n<p>    translator = Translator(from_lang=src_lang, to_lang=dest_lang)<\/p>\n<p>    translation = translator.translate(text)<\/p>\n<p>    return translation<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>id_text = &quot;Selamat datang di dunia Python&quot;<\/p>\n<p>translated_text = translate_id(id_text)<\/p>\n<p>print(translated_text)  # \u8f93\u51fa\uff1aWelcome to the world of Python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u81ea\u5b9a\u4e49\u51fd\u6570\u8fdb\u884cID\u7ffb\u8bd1<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u6709\u7279\u5b9a\u7684\u7ffb\u8bd1\u9700\u6c42\u6216\u8981\u5904\u7406\u7684\u6587\u672c\u6570\u636e\u91cf\u8f83\u5c0f\uff0c\u4f60\u4e5f\u53ef\u4ee5\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u8fdb\u884cID\u7ffb\u8bd1\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6620\u5c04\u7684\u65b9\u5f0f\u8fdb\u884c\u7b80\u5355\u7684ID\u7ffb\u8bd1\u3002<\/p>\n<\/p>\n<p><h4>1. \u7f16\u5199\u5b57\u5178\u6620\u5c04<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5305\u542bID\u548c\u5bf9\u5e94\u7ffb\u8bd1\u7684\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">translation_dict = {<\/p>\n<p>    &quot;Selamat&quot;: &quot;Welcome&quot;,<\/p>\n<p>    &quot;datang&quot;: &quot;to&quot;,<\/p>\n<p>    &quot;di&quot;: &quot;in&quot;,<\/p>\n<p>    &quot;dunia&quot;: &quot;world&quot;,<\/p>\n<p>    &quot;Python&quot;: &quot;Python&quot;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u7f16\u5199Python\u4ee3\u7801\u8fdb\u884c\u7ffb\u8bd1<\/h4>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u4f7f\u7528\u4e0a\u8ff0\u5b57\u5178\u8fdb\u884cID\u7ffb\u8bd1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def translate_id(text, translation_dict):<\/p>\n<p>    words = text.split()<\/p>\n<p>    translated_words = [translation_dict.get(word, word) for word in words]<\/p>\n<p>    return &#39; &#39;.join(translated_words)<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>id_text = &quot;Selamat datang di dunia Python&quot;<\/p>\n<p>translated_text = translate_id(id_text, translation_dict)<\/p>\n<p>print(translated_text)  # \u8f93\u51fa\uff1aWelcome to in world Python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u8c03\u7528API\u8fdb\u884cID\u7ffb\u8bd1\u3001\u4f7f\u7528\u5f00\u6e90\u5e93\u8fdb\u884cID\u7ffb\u8bd1\u3001\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u8fdb\u884cID\u7ffb\u8bd1<\/strong>\u662f\u4e09\u79cd\u5e38\u89c1\u7684Python\u7ffb\u8bd1ID\u7684\u65b9\u6cd5\u3002\u8c03\u7528API\u662f\u6700\u4e3a\u4fbf\u6377\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6587\u672c\u7ffb\u8bd1\uff1b\u4f7f\u7528\u5f00\u6e90\u5e93\u5219\u63d0\u4f9b\u4e86\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\uff1b\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u9002\u7528\u4e8e\u7279\u5b9a\u9700\u6c42\u7684\u5c0f\u89c4\u6a21\u6587\u672c\u7ffb\u8bd1\u3002\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u7ffb\u8bd1\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u4f7f\u7528Python\u8fdb\u884cID\u7ffb\u8bd1\uff0c\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u4e0b\u7684\u7ffb\u8bd1\u9700\u6c42\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u4f60\u5728Python\u7f16\u7a0b\u4e4b\u65c5\u4e2d\u53d6\u5f97\u66f4\u5927\u8fdb\u6b65\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u7528Python\u5b9e\u73b0\u591a\u8bed\u8a00\u7ffb\u8bd1\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5e93\u548cAPI\u63a5\u53e3\u6765\u5b9e\u73b0\u591a\u8bed\u8a00\u7ffb\u8bd1\u529f\u80fd\u3002\u6700\u5e38\u7528\u7684\u5e93\u662f<code>googletrans<\/code>\u548c<code>translate<\/code>\u3002\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff0c\u5e76\u8c03\u7528\u76f8\u5e94\u7684\u51fd\u6570\u6765\u8fdb\u884c\u6587\u672c\u7ffb\u8bd1\u3002\u6b64\u5916\uff0c\u4f7f\u7528Google\u7ffb\u8bd1\u7684API\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u7ffb\u8bd1\u9700\u6c42\uff0c\u5305\u62ec\u6279\u91cf\u7ffb\u8bd1\u548c\u81ea\u5b9a\u4e49\u7ffb\u8bd1\u6a21\u578b\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u7ffb\u8bd1\u65f6\u5982\u4f55\u5904\u7406\u7ffb\u8bd1\u8d28\u91cf\uff1f<\/strong><br \/>\u7ffb\u8bd1\u8d28\u91cf\u901a\u5e38\u53d6\u51b3\u4e8e\u6240\u4f7f\u7528\u7684\u7ffb\u8bd1\u5f15\u64ce\u3002\u4e3a\u4e86\u786e\u4fdd\u7ffb\u8bd1\u8d28\u91cf\uff0c\u7528\u6237\u53ef\u4ee5\u5728\u7ffb\u8bd1\u8fc7\u7a0b\u4e2d\u7ed3\u5408\u4e0a\u4e0b\u6587\uff0c\u4f7f\u7528\u66f4\u9ad8\u7ea7\u7684API\uff0c\u751a\u81f3\u5bf9\u7ffb\u8bd1\u7ed3\u679c\u8fdb\u884c\u4eba\u5de5\u6821\u5bf9\u3002\u901a\u8fc7\u5bf9\u6bd4\u4e0d\u540c\u7ffb\u8bd1\u5f15\u64ce\u7684\u7ed3\u679c\uff0c\u53ef\u4ee5\u9009\u62e9\u6700\u9002\u5408\u7684\u7ffb\u8bd1\u65b9\u6848\uff0c\u786e\u4fdd\u5185\u5bb9\u7684\u51c6\u786e\u6027\u548c\u81ea\u7136\u6d41\u7545\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u7ffb\u8bd1\u7279\u5b9a\u5b57\u6bb5\u7684ID\uff1f<\/strong><br \/>\u5728\u5904\u7406\u6570\u636e\u5e93\u6216\u6570\u636e\u6587\u4ef6\u65f6\uff0c\u7528\u6237\u53ef\u80fd\u9700\u8981\u7ffb\u8bd1\u7279\u5b9a\u5b57\u6bb5\u7684ID\u3002\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u904d\u5386\u6570\u636e\u96c6\u5e76\u8c03\u7528\u7ffb\u8bd1API\uff0c\u9010\u4e2a\u7ffb\u8bd1\u6240\u9700\u5b57\u6bb5\u3002\u7528\u6237\u8fd8\u53ef\u4ee5\u5229\u7528Pandas\u7b49\u6570\u636e\u5904\u7406\u5e93\uff0c\u5c06\u7ffb\u8bd1\u529f\u80fd\u96c6\u6210\u5230\u6570\u636e\u5904\u7406\u6d41\u7a0b\u4e2d\uff0c\u4ee5\u4fbf\u9ad8\u6548\u5730\u5904\u7406\u5927\u91cf\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u7528Python\u7ffb\u8bd1ID \u4f7f\u7528Python\u7ffb\u8bd1ID\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5982\u8c03\u7528API\u3001\u4f7f\u7528\u5f00\u6e90\u5e93\u3001\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u7b49 [&hellip;]","protected":false},"author":3,"featured_media":1024387,"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\/1024383"}],"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=1024383"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1024383\/revisions"}],"predecessor-version":[{"id":1024388,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1024383\/revisions\/1024388"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1024387"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1024383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1024383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1024383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}