python googletrans库 - 无法解码JSON对象

时间:2018-03-26 19:01:47

标签: python json python-2.7 google-translate

我正在创建一个可以帮助用户翻译文件的项目 从任何语言到英语。我正在使用python 2.7和googletrans库。

谷歌有一个限制,一次只允许翻译15k个字符,所以当用户输入大于15k的文件的路径时,程序会将其分成几个文件,运行请求分别翻译每个文件,保存每个文件中的翻译字符串并将其保存在新文件(翻译文件)中。

以下是程序决定文件是否足够大以分割它以及做什么的部分(如果没有):

file = open(path, "r")
string = file.read()
if len(string) > 15000: #15k maximum google server can translate
    file.close()
    devideFile(path)
    print "Large file, please wait.."
    output = translate_all(path)
    combineFile(path)
else:
    file.close()
    try:
        output = trans.translate(string).text
    except requests.exceptions.ConnectionError:
        print "Connection refused"
        print "You might not have internet connection"
        print "Or there's a problem with google translate server."

这是translate_all函数的翻译部分:

while True: #run as long as there are files left.
    try:
        temp = open(path + "(" + str(i) + ")" + "." + file_type, "r") #file(1).txt 
        string = temp.read() #for code readability
        try:
            output += (trans.translate(string).text) #for code readability (temp.read() => string)
        except requests.exceptions.ConnectionError:
            print "Connection refused"
            print "You might not have internet connection"
            print "Or there's a problem with google translate server."
        temp.close()
        i += 1
    except IOError:
        break
    sleep(1) #just so google wouldn't block client.
return output

问题是,翻译一个小文件是完美的,但当我尝试翻译一个大于15k字符的文件时,这就是输出:

Traceback (most recent call last):
File "translate.py", line 64, in <module>
main()
File "translate.py", line 47, in main
output = translate_all(path)
File "translate.py", line 23, in translate_all
output += (trans.translate(string).text) #for code readability (temp.read() => string)
File "/usr/local/lib/python2.7/dist-packages/googletrans/client.py", line 132, in translate
data = self._translate(text, dest, src)
File "/usr/local/lib/python2.7/dist-packages/googletrans/client.py", line 63, in _translate
data = utils.format_json(r.text)
File "/usr/local/lib/python2.7/dist-packages/googletrans/utils.py", line 62, in format_json
converted = legacy_format_json(original)
File "/usr/local/lib/python2.7/dist-packages/googletrans/utils.py", line 54, in legacy_format_json
converted = json.loads(text)
File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

我知道它很多,请帮忙:) 我在github上的项目:https://github.com/sisitrs2/fileTranslate

1 个答案:

答案 0 :(得分:-1)

显然字母限制是5k。 将if len(string) > 15000:更改为if len(string) > 5000(如果使用unicode,则更少)。