将与 Google 翻译服务交互的代码修改为列表中的 return 翻译数据,而不是打印它们
Ammend code interacting with Google translation service to return translated data in a list rather than priniting them
我正在尝试找到一种与 Google 翻译服务进行交互的方法。我希望能够向服务传递一个包含源语言字符串的列表,并返回一个包含目标语言值字符串的列表。
从 Python 开始工作,我在这里找到了一些相关代码:
https://github.com/google/google-api-python-client/blob/master/samples/translate/main.py
我试过代码,它有效。见下文:
from __future__ import print_function
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
from googleapiclient.discovery import build
def main():
# Build a service object for interacting with the API. Visit
# the Google APIs Console <http://code.google.com/apis/console>
# to get an API key for your own application.
service = build('translate', 'v2',
developerKey='AIzaSyCT4Ds5GuytOZnxGav492RAUvN508daobAVo')
print(service.translations().list(
source='el',
target='en',
q=['Είμαι πολύ ευτυχής που σας βλέπω σήμερα εδώ', 'Η κόρη μου θα χαρεί πολύ να σας δει']
).execute())
if __name__ == '__main__':
main()
当运行这段代码打印出以下输出:
{'translations': [{'translatedText': 'I am very happy to see you here today'}, {'translatedText': 'My daughter will be very pleased to see you'}]}
然而正如我所说,上面的代码将返回的结果打印到屏幕上。相反,我想要的是将翻译后的文本存储到一个对象中,最终我会把它变成一个 pandas 对象。
所以我尝试以这种方式对其进行修改 - 见下文:
from __future__ import print_function
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
from googleapiclient.discovery import build
def main():
# Build a service object for interacting with the API. Visit
# the Google APIs Console <http://code.google.com/apis/console>
# to get an API key for your own application.
service = build('translate', 'v2',
developerKey='AIzaSyCT4Ds5GuytOZnxGav492RAUvN508daobAVo')
result = service.translations().list(
source='el',
target='en',
q=['Είμαι πολύ ευτυχής που σας βλέπω σήμερα εδώ', 'Η κόρη μου θα χαρεί πολύ να σας δει']
).execute()
return result
if __name__ == '__main__':
main()
然而,当我尝试显示对象 "result" 时,出现以下异常:
NameError: name 'result' is not defined
你想"display result"怎么样?
看起来您的代码成功创建了结果,但是 main()
returns 它...到哪里?您如何调用 main 并使用它 returns?
您不能在定义它们的函数之外访问局部函数变量(例如结果)。
您可能希望此代码成为一个函数,将结果 returns 传递给另一个 function/program,然后使用返回的结果。
例如。
def your_main_program():
result = the_function_you_currently_have() #currently called main()
# do something with the result
我正在尝试找到一种与 Google 翻译服务进行交互的方法。我希望能够向服务传递一个包含源语言字符串的列表,并返回一个包含目标语言值字符串的列表。
从 Python 开始工作,我在这里找到了一些相关代码: https://github.com/google/google-api-python-client/blob/master/samples/translate/main.py
我试过代码,它有效。见下文:
from __future__ import print_function
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
from googleapiclient.discovery import build
def main():
# Build a service object for interacting with the API. Visit
# the Google APIs Console <http://code.google.com/apis/console>
# to get an API key for your own application.
service = build('translate', 'v2',
developerKey='AIzaSyCT4Ds5GuytOZnxGav492RAUvN508daobAVo')
print(service.translations().list(
source='el',
target='en',
q=['Είμαι πολύ ευτυχής που σας βλέπω σήμερα εδώ', 'Η κόρη μου θα χαρεί πολύ να σας δει']
).execute())
if __name__ == '__main__':
main()
当运行这段代码打印出以下输出:
{'translations': [{'translatedText': 'I am very happy to see you here today'}, {'translatedText': 'My daughter will be very pleased to see you'}]}
然而正如我所说,上面的代码将返回的结果打印到屏幕上。相反,我想要的是将翻译后的文本存储到一个对象中,最终我会把它变成一个 pandas 对象。
所以我尝试以这种方式对其进行修改 - 见下文:
from __future__ import print_function
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
from googleapiclient.discovery import build
def main():
# Build a service object for interacting with the API. Visit
# the Google APIs Console <http://code.google.com/apis/console>
# to get an API key for your own application.
service = build('translate', 'v2',
developerKey='AIzaSyCT4Ds5GuytOZnxGav492RAUvN508daobAVo')
result = service.translations().list(
source='el',
target='en',
q=['Είμαι πολύ ευτυχής που σας βλέπω σήμερα εδώ', 'Η κόρη μου θα χαρεί πολύ να σας δει']
).execute()
return result
if __name__ == '__main__':
main()
然而,当我尝试显示对象 "result" 时,出现以下异常:
NameError: name 'result' is not defined
你想"display result"怎么样?
看起来您的代码成功创建了结果,但是 main()
returns 它...到哪里?您如何调用 main 并使用它 returns?
您不能在定义它们的函数之外访问局部函数变量(例如结果)。
您可能希望此代码成为一个函数,将结果 returns 传递给另一个 function/program,然后使用返回的结果。
例如。
def your_main_program():
result = the_function_you_currently_have() #currently called main()
# do something with the result