gpt4 book ai didi

python - JSON解码错误 : using googletrans module

转载 作者:行者123 更新时间:2023-11-28 17:03:23 26 4
gpt4 key购买 nike

我正在尝试使用“googletrans”模块将 100,000 个英语单词翻译成韩语。但经过一些迭代后,它引发了

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

我试图弄明白,但网络上的解决方案对我不起作用。
我试过的是

  1. 每次迭代都重新初始化 Translator(),
  2. Time-sleep(.4) 每次迭代。

最奇怪的是它是随机发生的。有时它会在几百次迭代后出现,有时会在几次迭代后出现。

我查了这个模块的手册,我能找到的唯一限制是它只限制了单词的长度。但是,我要翻译的所有单词都只是字面上的单词,而不是短语或句子。

key_list = list(senticnet.keys())
for key in key_list:
translator = Translator()
time.sleep(.4)
print(translator.translate(key, dest='ko'))

上面代码中,senticnet是一个字典变量。看起来像

senticnet['abusive_conduct'] = ['0', '0', '0.853', '-0.84', '#anger', '#disgust', 'negative', '-0.84', 'flagrant', 'cry', 'glaring', 'gross', 'rank']
senticnet['abusive_father'] = ['0', '0', '0.821', '-0.95', '#anger', '#disgust', 'negative', '-0.88', 'student', 'serious_student', 'addiction', 'graduate_student', 'hard_worker']

这是错误信息

/content/gdrive/My Drive/Colab Notebooks/py-googletrans/googletrans/client.py in translate(self, text, dest, src)
170
171 origin = text
--> 172 data = self._translate(text, dest, src)
173
174 # this code will be updated when the format is changed.

/content/gdrive/My Drive/Colab Notebooks/py-googletrans/googletrans/client.py in _translate(self, text, dest, src)
79 r = self.session.get(url, params=params)
80
---> 81 data = utils.format_json(r.text)
82 return data
83

/content/gdrive/My Drive/Colab Notebooks/py-googletrans/googletrans/utils.py in format_json(original)
60 converted = json.loads(original)
61 except ValueError:
---> 62 converted = legacy_format_json(original)
63
64 return converted

/content/gdrive/My Drive/Colab Notebooks/py-googletrans/googletrans/utils.py in legacy_format_json(original)
52 text = text[:p] + states[j][1] + text[nxt:]
53
---> 54 converted = json.loads(text)
55 return converted
56

/usr/lib/python3.6/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
352 parse_int is None and parse_float is None and
353 parse_constant is None and object_pairs_hook is None and not kw):
--> 354 return _default_decoder.decode(s)
355 if cls is None:
356 cls = JSONDecoder

/usr/lib/python3.6/json/decoder.py in decode(self, s, _w)
337
338 """
--> 339 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
340 end = _w(s, end).end()
341 if end != len(s):

/usr/lib/python3.6/json/decoder.py in raw_decode(self, s, idx)
355 obj, end = self.scan_once(s, idx)
356 except StopIteration as err:
--> 357 raise JSONDecodeError("Expecting value", s, err.value) from None
358 return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

最佳答案

Question: I checked manual of this module and the only limit I could find was that it only limits the length of the word.


还有一些其他限制!

From Googletrans 2.3.0 documentation

Note on library usage

  • Due to limitations of the web version of google translate, this API does not guarantee that the library would work properly at all times. (so please use this library if you don’t care about stability.)
  • If you want to use a stable API, I highly recommend you to use Google’s official translate API.
  • If you get HTTP 5xx error or errors like #6, it’s probably because Google has banned your client IP address.

关于python - JSON解码错误 : using googletrans module,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52809016/

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