[英]How to decode this error? I'm using Google API for translation of a list using python
How to decode this error? 如何解码此错误? I'm using Google API for translation of a list using Python:
我正在使用Google API使用Python翻译列表:
from googletrans import Translator
import json
#intentional conversion
translator=Translator()
z=[]
translations=translator.translate(['
ik ben goed','guten tag','das ist ein junge'], dest='en')
possible_json_string = str(translations)
possible_json_string = '{}' #sanity check with simplest json
possible_json_string = translations #why convert to string at all?
possible_json_string = translations.decode('utf-8')
for translation in translations:
print(translation.origin, ' -> ', translation.text)
z.append(translation.text)
It's not clear what you're trying to achieve with possible_json_string
. 目前尚不清楚你想要达到什么
possible_json_string
。 All you need is the following code: 您只需要以下代码:
from googletrans import Translator
translator=Translator()
translations=translator.translate(['ik ben goed','guten tag','das ist ein junge'], dest='en')
z = [] # assuming you'll use this list further down the line
for translation in translations:
print(translation.origin, ' -> ', translation.text)
z.append(translation.text)
Output: 输出:
ik ben goed -> I'm good
guten tag -> good day
das ist ein junge -> this is a boy
The problem here is that translations is a list which do not have decode() function. 这里的问题是, 翻译是一个没有decode()函数的列表。 Actually you do not need this step as in python3 all strings are automatically Unicode.
实际上,您不需要此步骤,因为在python3中所有字符串都是自动Unicode的。 If you want to apply a function all element of a list you can use map, for example:
如果要应用功能,可以使用map的所有元素,例如:
list(map(lambda x: x.text.upper(), translations))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.