如何解码这个错误?I';我使用GoogleAPI使用python翻译列表
如何解码这个错误?我正在使用Google API使用Python翻译列表:如何解码这个错误?I';我使用GoogleAPI使用python翻译列表,python,python-3.x,Python,Python 3.x,如何解码这个错误?我正在使用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_js
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)
现在还不清楚您使用
可能的\u json\u string
想要实现什么。您只需要以下代码:
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)
输出:
ik ben goed -> I'm good
guten tag -> good day
das ist ein junge -> this is a boy
这里的问题是翻译是一个没有decode()函数的列表。实际上,您不需要这个步骤,因为在python3中,所有字符串都自动使用Unicode。
如果要应用函数列表的所有元素,可以使用map,例如:
list(map(lambda x: x.text.upper(), translations))
怎么了?你得到了什么输出,你期望得到什么输出?问题是它显示了一个json解码错误,我们如何解决这个错误?