简体   繁体   English

googletrans 字典翻译错误

[英]googletrans error with dictionary translate

Trying to use googletrans in my project, using version 4.0.0-rc.1, Python 13.10 on Windows 10 local machine.尝试在我的项目中使用 googletrans,在 Windows 10 本地机器上使用版本 4.0.0-rc.1、Python 13.10。

The script is simple, it must translate an array of the phrases into English from Russian.脚本很简单,它必须将一组短语从俄语翻译成英语。 When trying to translate one phrase - it's ok, but when trying to translate a dictionary - the error occurs:尝试翻译一个短语时 - 没关系,但尝试翻译字典时 - 发生错误:

Traceback (most recent call last):
  File "c:\Users\fire\Dropbox\my_soft\python_code_lessons\py_version.py", line 107, in <module>
    translated = tr.translate(data, dest='en')
  File "C:\Users\fire\Dropbox\my_soft\python_code_lessons\venv\lib\site-packages\googletrans\client.py", line 219, in translate
    parsed = json.loads(data[0][2])
  File "C:\Users\fire\AppData\Local\Programs\Python\Python310\lib\json\__init__.py", line 339, in loads
    raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not NoneType

Part of the script where this error occurs:发生此错误的部分脚本:

import googletrans
from googletrans import Translator
print(googletrans.__version__)

tr = Translator()
data = ['привет', 'мой мир', 'лучший']

translated = tr.translate(data, dest='en')

for trans in translated:
    print(f'{trans.origin} -> {trans.text}')

Where should I dig?我应该在哪里挖? Or maybe someone had such a problem?或者也许有人有这样的问题?

This seems to be the problem of this particular API not the google translation itself.这似乎是这个特定的 API 的问题,而不是谷歌翻译本身的问题。 I found that this issue has been logged to py-googletrans github and does not seem to be resolved yet.我发现这个问题已经记录到py-googletrans github并且似乎还没有解决。

I do not know this API at all, but when I looked into the code on githb the file mentioned in the error message googletrans\client.py seems to be changed as line 219 is completely different to the part in the error.我根本不知道这个 API,但是当我查看 githb 上的代码时,错误消息googletrans\client.py中提到的文件似乎已更改,因为第 219 行与错误中的部分完全不同。 Maybe some update will solve the case...也许一些更新会解决这个问题......

Anyway you should try Google APIs for Transactions.无论如何,您应该尝试 Google APIs for Transactions。 There are 3 of them available and all have Python API so it should match your needs.其中有 3 个可用,并且都有 Python API 所以它应该符合您的需求。

You may find comparison of them here .你可以在这里找到它们的比较。

Basic and Advance Translation API has nice python quickstarts in the documentations .基本和高级翻译 API 在文档中有很好的 python 快速入门。

AutoML Translation API is quite hard to find so please check here . AutoML Translation API 很难找到,所以请在此处查看 It does not contain so nice Python quickstart however there is nice samles part where you can find nice python examples like this one .它不包含那么好的 Python 快速入门,但是有一个不错的示例部分,您可以在其中找到像这样的不错的 python 示例。

As mentioned by @vitooh, there is a known issue on the googletrans library and is yet to be resolved.正如@vitooh 所提到的, googletrans库中有一个已知问题,尚未解决。 You may check the given references of @vitooh for more information.您可以查看@vitooh 的给定参考资料以获取更多信息。

As a work around on your case, you may use the code below:作为解决您的案例的方法,您可以使用以下代码:

import googletrans
from googletrans import Translator
print(googletrans.__version__)

tr = Translator()
data = ['привет', 'мой мир', 'лучший']
for lang in data:
    translated = tr.translate(lang, dest='en')
    print(f'{translated.origin} -> {translated.text}')

The code loops the given data so that it will be translated per String.代码循环给定数据,以便按字符串进行翻译。

Please see below screenshot of my testing for your reference:请看下面我的测试截图供您参考:

测试证明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 googletrans 翻译 Pandas df,AttributeError 错误 - Translate a Pandas df using googletrans, AttributeError error 尝试使用 googletrans 翻译内容时出现错误 - I get an error when I try to translate something with googletrans googletrans 不翻译数字 - googletrans doesn't translate the numbers 在 python translate() 中使用 googletrans api 时出错,缺少 1 个必需的位置参数:&#39;self&#39; - Error using googletrans api in python translate() missing 1 required positional argument: 'self' 如何在python中使用googletrans API翻译网页? - How to Translate a webpage with googletrans API in python? 如何使用 googletrans 在 Python 中翻译 Pandas 系列? - How to translate a Pandas Series in Python using googletrans? 我正在使用 python3,我想翻译一些文本,所以我使用了“googletrans”package,但我收到了一些错误 - I am using python3 and I want to translate some text, So I used “googletrans” package, but I am getting some error 安装 googletrans 时遇到错误 - Stuck with error while installing googletrans 是否可以使用 googletrans 只翻译标记区域之间的内容? - Is it possible to use googletrans to only translate whats between marked areas? Python googletrans 输出与网络版 Google Translate 不同 - Python googletrans output differs from the web version of Google Translate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM