【发布时间】:2018-06-09 19:43:20
【问题描述】:
我在 Stack Overflow 上进行了彻底的搜索,但找不到这个问题的答案。我正在尝试将 Google Translate API (googletrans 2.2.0) 用于 Python (3.6.2),并尝试将一组非英语文档翻译成英语。我让谷歌翻译做语言检测。这是我的代码:
## 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)
有人可以帮忙吗?
【问题讨论】:
-
这里有两个错误:1. 如果
p是一个字典,str(p)将给出一个无效的 JSON 字符串。 2.如果p是原始JSON字符串,p[:1500]会分块JSON字符串,使其成为无效的JSON字符串。 -
感谢 Arount。奇怪的是,当我删除“p = str(p[:15000])”这一行时,我得到了完全相同的 JSON 解码器错误......关于可能发生的任何其他想法?
标签: python python-3.x google-translate language-translation jsondecoder