【问题标题】:How to fix 'translate() got an unexpected keyword argument 'format''如何修复'translate()得到一个意外的关键字参数'格式''
【发布时间】:2019-09-18 04:08:19
【问题描述】:

我在翻译返回撇号的文本时遇到问题,例如 en: "this is me" == fr: "c'est moi",但我得到" c'est moi"。 为此,我想将格式指定为文本,但是当我执行脚本时,我得到:

TypeError: translate() got an unexpected keyword argument 'format'
from google.cloud import translate

# Instantiates a client
translate_client = translate.Client()

# The text to translate
text = u'this is me'
# The target language
target = 'fr'

# Translates some text into Russian
translation = translate_client.translate(
    text,
    target_language=target, format='text')

print(u'Text: {}'.format(text))
print(u'Translation: {}'.format(translation['translatedText']))

【问题讨论】:

  • 错误似乎是正确的。根据documentation,没有名为format的关键字。
  • 那么我如何让 api 返回撇号
  • 您可以使用_format 而不是format。它有一个前导下划线。 @Matthias 检查代码 here.
  • @user9477447 API 的返回是正确的,format_ 对您没有帮助,因为它用于 input 而不是 output,但我提供了一种在下面为您转换它的方法。

标签: python google-translation-api


【解决方案1】:

如果你想为format 提供一个参数,你必须使用format_,它接受可选参数:[github code]

:type format_:str

:param format_:(可选)texthtml 之一,用于指定输入文本是纯文本还是 HTML。

但是这是 输入文本 不是输出文本。如果你想转换回一个真正的撇号,你可以使用html.unescape,因为你得到的是字符的 html 表示:[docs]

import html
print(html.unescape(text))

【讨论】:

  • 太棒了。让我知道您是否需要其他任何东西。 :)
猜你喜欢
  • 2019-12-19
  • 1970-01-01
  • 1970-01-01
  • 2019-06-17
  • 2016-09-17
  • 2015-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多