谷歌翻译 V2 样本代码并没有按预期工作

2024-09-24 22:23:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试了googletranslate上提供的Python示例代码,它允许文本翻译

我在尝试google Translate的python示例代码时遇到以下错误,该代码取自这里https://cloud.google.com/translate/docs/basic/translating-text#translating_text

dailydiy@linuxbox:~$ export GOOGLE_APPLICATION_CREDENTIALS="/home/dailydiy/WrittenAudio/credentials/credentials.json"
dailydiy@linuxbox:~$ python3 testtranslate.py 
Traceback (most recent call last):
  File "testtranslate.py", line 2, in <module>
    translate_client = translate.Client()
  File "/home/dailydiy/.local/lib/python3.6/site-packages/google/cloud/translate_v2/client.py", line 94, in __init__
    self._connection = Connection(self, **kw_args)
  File "/home/dailydiy/.local/lib/python3.6/site-packages/google/cloud/translate_v2/_http.py", line 35, in __init__
    super(Connection, self).__init__(client, client_info)
TypeError: __init__() takes 2 positional arguments but 3 were given

我的要求.txt文件包含以下条目

google-cloud-translate==2.0.0

我的代码与示例代码完全相同

from google.cloud import translate_v2 as translate
translate_client = translate.Client()

if isinstance(text, six.binary_type):
    text = text.decode('utf-8')

# Text can also be a sequence of strings, in which case this method
# will return a sequence of results for each text.
result = translate_client.translate(
    text, target_language=target)

print(u'Text: {}'.format(result['input']))
print(u'Translation: {}'.format(result['translatedText']))
print(u'Detected source language: {}'.format(
    result['detectedSourceLanguage']))

似乎API和示例代码不同步,并且在尝试初始化客户机的行失败。我知道这个错误意味着参数的数量不匹配,但我似乎无法理解参数的位置。你知道吗


Tags: 代码textinpyclientcloud示例home