{"id":1045732,"date":"2024-12-31T13:26:37","date_gmt":"2024-12-31T05:26:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1045732.html"},"modified":"2024-12-31T13:26:42","modified_gmt":"2024-12-31T05:26:42","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e8%bd%ac%e6%8d%a2%e6%88%90%e4%b8%ad%e6%96%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1045732.html","title":{"rendered":"python\u5982\u4f55\u5c06\u8f6c\u6362\u6210\u4e2d\u6587"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/8004730d-657b-44dd-aad1-bdafa672b482.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5c06\u8f6c\u6362\u6210\u4e2d\u6587\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u5c06\u6587\u672c\u8f6c\u6362\u6210\u4e2d\u6587\uff0c\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528Google\u7ffb\u8bd1API\u3001\u4f7f\u7528\u767e\u5ea6\u7ffb\u8bd1API\u3001\u4f7f\u7528\u7ffb\u8bd1\u5e93(\u5982: googletrans\u3001translate)\u7b49\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528googletrans\u5e93\u662f\u4e00\u4e2a\u65b9\u4fbf\u4e14\u7b80\u5355\u7684\u65b9\u6cd5\u3002googletrans\u662f\u4e00\u4e2a\u975e\u5b98\u65b9\u7684Google\u7ffb\u8bd1API\u5ba2\u6237\u7aef\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u6587\u672c\u7ffb\u8bd1\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>googletrans\u5e93\u7684\u4f7f\u7528\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u5b89\u88c5\u5e93\u5e76\u8c03\u7528\u76f8\u5173\u65b9\u6cd5\u5373\u53ef\u5b8c\u6210\u7ffb\u8bd1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8be6\u7ec6\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from googletrans import Translator<\/p>\n<p>def translate_to_chinese(text):<\/p>\n<p>    translator = Translator()<\/p>\n<p>    translated = translator.translate(text, dest=&#39;zh-cn&#39;)<\/p>\n<p>    return translated.text<\/p>\n<h2><strong>\u793a\u4f8b\u6587\u672c<\/strong><\/h2>\n<p>text = &quot;Hello, how are you?&quot;<\/p>\n<p>translated_text = translate_to_chinese(text)<\/p>\n<p>print(translated_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e00\u6bb5\u4ee3\u7801\u901a\u8fc7\u5b9e\u4f8b\u5316<code>Translator<\/code>\u5bf9\u8c61\uff0c\u5e76\u8c03\u7528<code>translate<\/code>\u65b9\u6cd5\u5c06\u6587\u672c\u7ffb\u8bd1\u6210\u4e2d\u6587\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4ed6\u65b9\u6cd5\u548c\u66f4\u591a\u4f7f\u7528\u6280\u5de7\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Google\u7ffb\u8bd1API<\/h3>\n<\/p>\n<p><p>Google\u7ffb\u8bd1API\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u7ffb\u8bd1\u670d\u52a1\uff0c\u652f\u6301\u591a\u79cd\u8bed\u8a00\u7684\u76f8\u4e92\u7ffb\u8bd1\u3002\u8981\u4f7f\u7528Google\u7ffb\u8bd1API\uff0c\u9700\u8981\u8fdb\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u83b7\u53d6API\u5bc6\u94a5<\/strong>\uff1a\u5728Google Cloud\u5e73\u53f0\u4e0a\u521b\u5efa\u4e00\u4e2a\u9879\u76ee\uff0c\u5e76\u542f\u7528\u7ffb\u8bd1API\u3002\u7136\u540e\u751f\u6210API\u5bc6\u94a5\u3002<\/li>\n<li><strong>\u5b89\u88c5\u6240\u9700\u5e93<\/strong>\uff1a\u901a\u8fc7pip\u5b89\u88c5<code>google-cloud-translate<\/code>\u5e93\u3002<\/li>\n<li><strong>\u7f16\u5199\u4ee3\u7801<\/strong>\uff1a\u4f7f\u7528API\u5bc6\u94a5\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\uff0c\u5e76\u8c03\u7528\u7ffb\u8bd1\u65b9\u6cd5\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from google.cloud import translate_v2 as translate<\/p>\n<p>def translate_text(text, target_language=&#39;zh&#39;):<\/p>\n<p>    translate_client = translate.Client()<\/p>\n<p>    result = translate_client.translate(text, target_language=target_language)<\/p>\n<p>    return result[&#39;translatedText&#39;]<\/p>\n<h2><strong>\u8bbe\u7f6eAPI\u5bc6\u94a5<\/strong><\/h2>\n<p>import os<\/p>\n<p>os.environ[&quot;GOOGLE_APPLICATION_CREDENTIALS&quot;] = &quot;path_to_your_api_key.json&quot;<\/p>\n<h2><strong>\u793a\u4f8b\u6587\u672c<\/strong><\/h2>\n<p>text = &quot;Hello, how are you?&quot;<\/p>\n<p>translated_text = translate_text(text)<\/p>\n<p>print(translated_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u767e\u5ea6\u7ffb\u8bd1API<\/h3>\n<\/p>\n<p><p>\u767e\u5ea6\u7ffb\u8bd1API\u4e5f\u662f\u4e00\u4e2a\u5e38\u7528\u7684\u7ffb\u8bd1\u670d\u52a1\uff0c\u652f\u6301\u591a\u79cd\u8bed\u8a00\u7684\u76f8\u4e92\u7ffb\u8bd1\u3002\u8981\u4f7f\u7528\u767e\u5ea6\u7ffb\u8bd1API\uff0c\u9700\u8981\u8fdb\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u6ce8\u518c\u767e\u5ea6\u7ffb\u8bd1API<\/strong>\uff1a\u5728\u767e\u5ea6\u7ffb\u8bd1\u5f00\u653e\u5e73\u53f0\u6ce8\u518c\u8d26\u53f7\uff0c\u521b\u5efa\u5e94\u7528\u5e76\u83b7\u53d6App ID\u548c\u5bc6\u94a5\u3002<\/li>\n<li><strong>\u5b89\u88c5\u6240\u9700\u5e93<\/strong>\uff1a\u901a\u8fc7pip\u5b89\u88c5<code>requests<\/code>\u5e93\u3002<\/li>\n<li><strong>\u7f16\u5199\u4ee3\u7801<\/strong>\uff1a\u4f7f\u7528App ID\u548c\u5bc6\u94a5\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\uff0c\u5e76\u8c03\u7528\u7ffb\u8bd1\u65b9\u6cd5\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>import hashlib<\/p>\n<p>import random<\/p>\n<p>def translate_to_chinese(text, appid, secret_key):<\/p>\n<p>    url = &quot;https:\/\/fanyi-api.b<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>du.com\/api\/trans\/vip\/translate&quot;<\/p>\n<p>    from_lang = &quot;auto&quot;<\/p>\n<p>    to_lang = &quot;zh&quot;<\/p>\n<p>    salt = str(random.randint(32768, 65536))<\/p>\n<p>    sign = hashlib.md5((appid + text + salt + secret_key).encode(&#39;utf-8&#39;)).hexdigest()<\/p>\n<p>    params = {<\/p>\n<p>        &quot;q&quot;: text,<\/p>\n<p>        &quot;from&quot;: from_lang,<\/p>\n<p>        &quot;to&quot;: to_lang,<\/p>\n<p>        &quot;appid&quot;: appid,<\/p>\n<p>        &quot;salt&quot;: salt,<\/p>\n<p>        &quot;sign&quot;: sign<\/p>\n<p>    }<\/p>\n<p>    response = requests.get(url, params=params)<\/p>\n<p>    result = response.json()<\/p>\n<p>    return result[&#39;trans_result&#39;][0][&#39;dst&#39;]<\/p>\n<h2><strong>\u8bbe\u7f6eApp ID\u548c\u5bc6\u94a5<\/strong><\/h2>\n<p>appid = &#39;your_appid&#39;<\/p>\n<p>secret_key = &#39;your_secret_key&#39;<\/p>\n<h2><strong>\u793a\u4f8b\u6587\u672c<\/strong><\/h2>\n<p>text = &quot;Hello, how are you?&quot;<\/p>\n<p>translated_text = translate_to_chinese(text, appid, secret_key)<\/p>\n<p>print(translated_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u7ffb\u8bd1\u5e93\uff08googletrans\u3001translate\uff09<\/h3>\n<\/p>\n<p><h4>\u4f7f\u7528googletrans\u5e93<\/h4>\n<\/p>\n<p><p>googletrans\u5e93\u662f\u4e00\u4e2a\u975e\u5b98\u65b9\u7684Google\u7ffb\u8bd1API\u5ba2\u6237\u7aef\uff0c\u4f7f\u7528\u8d77\u6765\u975e\u5e38\u65b9\u4fbf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from googletrans import Translator<\/p>\n<p>def translate_to_chinese(text):<\/p>\n<p>    translator = Translator()<\/p>\n<p>    translated = translator.translate(text, dest=&#39;zh-cn&#39;)<\/p>\n<p>    return translated.text<\/p>\n<h2><strong>\u793a\u4f8b\u6587\u672c<\/strong><\/h2>\n<p>text = &quot;Hello, how are you?&quot;<\/p>\n<p>translated_text = translate_to_chinese(text)<\/p>\n<p>print(translated_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528translate\u5e93<\/h4>\n<\/p>\n<p><p>translate\u5e93\u662f\u4e00\u4e2a\u7b80\u5355\u6613\u7528\u7684\u7ffb\u8bd1\u5e93\uff0c\u652f\u6301\u591a\u79cd\u7ffb\u8bd1\u670d\u52a1\uff08\u5982Google\u3001Microsoft\u3001Yandex\u7b49\uff09\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from translate import Translator<\/p>\n<p>def translate_to_chinese(text):<\/p>\n<p>    translator = Translator(to_lang=&quot;zh&quot;)<\/p>\n<p>    translated = translator.translate(text)<\/p>\n<p>    return translated<\/p>\n<h2><strong>\u793a\u4f8b\u6587\u672c<\/strong><\/h2>\n<p>text = &quot;Hello, how are you?&quot;<\/p>\n<p>translated_text = translate_to_chinese(text)<\/p>\n<p>print(translated_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5176\u4ed6\u6280\u5de7\u548c\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li><strong>\u9009\u62e9\u5408\u9002\u7684\u7ffb\u8bd1\u670d\u52a1<\/strong>\uff1a\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u7ffb\u8bd1\u670d\u52a1\u3002\u5982\u679c\u9700\u8981\u9ad8\u8d28\u91cf\u7684\u7ffb\u8bd1\uff0c\u53ef\u4ee5\u9009\u62e9Google\u7ffb\u8bd1API\u6216\u767e\u5ea6\u7ffb\u8bd1API\u3002\u5982\u679c\u53ea\u662f\u7b80\u5355\u7684\u7ffb\u8bd1\u4efb\u52a1\uff0c\u53ef\u4ee5\u4f7f\u7528googletrans\u6216translate\u5e93\u3002<\/li>\n<li><strong>\u5904\u7406\u7279\u6b8a\u5b57\u7b26\u548c\u683c\u5f0f<\/strong>\uff1a\u5728\u8fdb\u884c\u7ffb\u8bd1\u65f6\uff0c\u6ce8\u610f\u5904\u7406\u6587\u672c\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26\u548c\u683c\u5f0f\uff0c\u4ee5\u907f\u514d\u7ffb\u8bd1\u7ed3\u679c\u51fa\u73b0\u95ee\u9898\u3002<\/li>\n<li><strong>\u6279\u91cf\u7ffb\u8bd1<\/strong>\uff1a\u5982\u679c\u9700\u8981\u7ffb\u8bd1\u5927\u91cf\u6587\u672c\uff0c\u53ef\u4ee5\u5c06\u6587\u672c\u5206\u5757\u5904\u7406\uff0c\u907f\u514d\u4e00\u6b21\u6027\u7ffb\u8bd1\u8fc7\u591a\u5185\u5bb9\u5bfc\u81f4API\u8c03\u7528\u5931\u8d25\u3002<\/li>\n<li><strong>\u7f13\u5b58\u7ffb\u8bd1\u7ed3\u679c<\/strong>\uff1a\u5bf9\u4e8e\u9891\u7e41\u9700\u8981\u7ffb\u8bd1\u7684\u6587\u672c\uff0c\u53ef\u4ee5\u8003\u8651\u7f13\u5b58\u7ffb\u8bd1\u7ed3\u679c\uff0c\u4ee5\u63d0\u9ad8\u6548\u7387\u5e76\u51cf\u5c11API\u8c03\u7528\u6b21\u6570\u3002<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Python\u5c06\u6587\u672c\u8f6c\u6362\u6210\u4e2d\u6587\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u7ffb\u8bd1\u6548\u7387\u548c\u51c6\u786e\u6027\u3002\u65e0\u8bba\u662f\u4f7f\u7528Google\u7ffb\u8bd1API\u3001\u767e\u5ea6\u7ffb\u8bd1API\uff0c\u8fd8\u662f\u4f7f\u7528googletrans\u3001translate\u5e93\uff0c\u90fd\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u6587\u672c\u7ffb\u8bd1\u529f\u80fd\u3002\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u5e0c\u671b\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u4f7f\u7528Python\u8fdb\u884c\u6587\u672c\u7ffb\u8bd1\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\u5b57\u7b26\u7684\u7f16\u7801\u548c\u89e3\u7801\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>encode()<\/code>\u548c<code>decode()<\/code>\u65b9\u6cd5\u6765\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u7684\u7f16\u7801\u548c\u89e3\u7801\u3002\u901a\u5e38\uff0cUTF-8\u7f16\u7801\u662f\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u7684\u5e38\u7528\u65b9\u5f0f\u3002\u4f7f\u7528<code>str.encode(&#39;utf-8&#39;)<\/code>\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\uff0c\u4f7f\u7528<code>bytes.decode(&#39;utf-8&#39;)<\/code>\u53ef\u4ee5\u5c06\u5b57\u8282\u8f6c\u6362\u56de\u4e2d\u6587\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u5904\u7406\u4e2d\u6587\u6587\u4ef6\u7684\u8bfb\u5199\uff1f<\/strong><br \/>\u5728\u5904\u7406\u5305\u542b\u4e2d\u6587\u7684\u6587\u4ef6\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528UTF-8\u7f16\u7801\u6765\u8bfb\u5199\u6587\u4ef6\u3002\u53ef\u4ee5\u901a\u8fc7<code>open()<\/code>\u51fd\u6570\u7684<code>encoding<\/code>\u53c2\u6570\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\uff0c\u4f8b\u5982\uff1a<code>open(&#39;file.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;)<\/code>\u3002\u8fd9\u6837\u80fd\u786e\u4fdd\u4e2d\u6587\u5b57\u7b26\u88ab\u6b63\u786e\u8bfb\u53d6\u548c\u5199\u5165\uff0c\u907f\u514d\u51fa\u73b0\u4e71\u7801\u95ee\u9898\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5904\u7406\u4e2d\u6587\u6587\u672c\uff1f<\/strong><br \/>\u8bb8\u591a\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982<code>pandas<\/code>\u548c<code>jieba<\/code>\uff0c\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u4e2d\u6587\u6587\u672c\u5904\u7406\u529f\u80fd\u3002<code>pandas<\/code>\u53ef\u4ee5\u7528\u4e8e\u6570\u636e\u5206\u6790\uff0c\u652f\u6301\u4e2d\u6587\u6570\u636e\u7684\u8bfb\u53d6\u548c\u5904\u7406\uff0c\u800c<code>jieba<\/code>\u5219\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u4e2d\u6587\u5206\u8bcd\u5e93\uff0c\u80fd\u591f\u5e2e\u52a9\u7528\u6237\u5c06\u4e2d\u6587\u53e5\u5b50\u5207\u5206\u4e3a\u8bcd\u8bed\uff0c\u65b9\u4fbf\u540e\u7eed\u7684\u6587\u672c\u5206\u6790\u4e0e\u5904\u7406\u3002\u4f7f\u7528\u8fd9\u4e9b\u5e93\u65f6\uff0c\u786e\u4fdd\u5b89\u88c5\u5e76\u5bfc\u5165\u76f8\u5e94\u7684\u6a21\u5757\uff0c\u4ee5\u4fbf\u987a\u5229\u8fdb\u884c\u4e2d\u6587\u6587\u672c\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u5c06\u6587\u672c\u8f6c\u6362\u6210\u4e2d\u6587\uff0c\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528Google\u7ffb\u8bd1API\u3001\u4f7f\u7528\u767e\u5ea6\u7ffb\u8bd1API\u3001\u4f7f\u7528\u7ffb\u8bd1\u5e93(\u5982 [&hellip;]","protected":false},"author":3,"featured_media":1045760,"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\/1045732"}],"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=1045732"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1045732\/revisions"}],"predecessor-version":[{"id":1045761,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1045732\/revisions\/1045761"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1045760"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1045732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1045732"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1045732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}