gpt4 book ai didi

python - JSONDecodeError 在 Python3 中使用谷歌翻译 API

转载 作者:太空狗 更新时间:2023-10-30 00:12:54 25 4
gpt4 key购买 nike

我在 Stack Overflow 上进行了全面搜索,但找不到此问题的答案。我正在尝试使用适用于 Python (3.6.2) 的 Google Translate API (googletrans 2.2.0),并尝试将一组非英语文档翻译成英语。我让谷歌翻译进行语言检测。这是我的代码:

## newcorpus is a corpus I have created consisting of non-english documents
fileids = newcorpus.fileids
for f in fileids:
p = newcorpus.raw(f)
p = str(p[:15000])
translated_text = translator.translate(p)
print(translated_text)
sleep(10)

我通过每次等待 10 秒来限制对 API 的调用。我也一次只向 API 提供 15k 个字符以保持在字符数限制内。

每次运行此代码时,我都会收到以下错误消息:

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

有人可以帮忙吗?

最佳答案

我想我可能已经找到了我自己问题的答案。如果我将一次提供给 API 的字符数减少到 5k,一切似乎都正常。很奇怪,因为 Googletrans 文档说限制是 15k...嗯。我将不得不对请求进行批处理。

关于python - JSONDecodeError 在 Python3 中使用谷歌翻译 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48021371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com