我在 Stack Overflow 上进行了彻底的搜索,但没有找到解决这个问题的答案。我正在尝试使用适用于 Python(3.6.2)的 Google Translate API(googletrans 2.2.0),并尝试将一组非英语文档翻译成英语。我让 Google Translate 进行语言检测。以下是我的代码:
## 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)
有人能帮忙吗?
p
是一个字典,str(p)
将会生成一个非法的 JSON 字符串。2. 如果p
是一个原始的 JSON 字符串,p[:1500]
会将 JSON 字符串分块,使其成为非法的 JSON 字符串。 - Arount