python googletrans 库 - 无法解码 JSON 对象
python googletrans library - No JSON object could be decoded
我正在创建一个帮助用户翻译文件的项目
从任何语言到英语。我正在使用 python 2.7 和 googletrans 库。
google 有一个限制,一次只能翻译 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
显然字母限制是 5k。
将 if len(string) > 15000:
更改为 if len(string) > 5000
(如果使用 unicode,则更少)。
我正在创建一个帮助用户翻译文件的项目 从任何语言到英语。我正在使用 python 2.7 和 googletrans 库。
google 有一个限制,一次只能翻译 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
显然字母限制是 5k。
将 if len(string) > 15000:
更改为 if len(string) > 5000
(如果使用 unicode,则更少)。