简体   繁体   English

如何解码此错误? 我正在使用Google API使用python翻译列表

[英]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.

相关问题 如何在 Python 中使用谷歌云翻译 API 翻译大文本? - How to translate large text using Google Cloud Translation API in Python? 如何使用 python 在 Google colab 中解码文件? - How to decode the file in Google colab using python? 如何在 Django 和 python 中使用谷歌翻译 API - How can I use google translation API in Django with python 如何使用Python使用Google Map Feed API获取我的Google Maps列表? - How can I use Google Map Feed API to get a list of my Google Maps using Python? Google API翻译错误 - Google api translation error 使用Python和Google Vision检测PDF文件上的文本时,我收到JSON解码错误 - I get an JSON decode error when using Python and Google Vision to detect text on PDF file Python 如何使用谷歌翻译翻译口语成绩单 - How to translate spoken transcript using google translation by Python Excel 工作簿翻译使用谷歌 API - Excel workbook translation using Google API 通过 Python 使用谷歌翻译的最佳方式 - Best way of using google translation by Python 当我尝试使用 python 创建空白文档时,Google API 未在驱动器中创建任何文档 - Google API is not creating any document in the drive when I'm trying to create a blank document using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM