使用pygoogletrans时出现JSONDecode错误

时间:2019-11-25 15:40:28

标签: python google-translation-api

我正在尝试使用pygoogletrans翻译多语言数据集。经过一番搜索后,我发现从字符串中删除表情符号后,“ JSONDecode错误”已解决。

但是现在我运行以下代码:

translate = Translator()
translate.translate("Dit is een test")

我收到以下错误:

JSONDecodeError                           Traceback (most recent call last)
<command-4102236279622906> in <module>
      1 translate = Translator()
----> 2 translate.translate("Dit is een test")

/databricks/python/lib/python3.7/site-packages/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.

/databricks/python/lib/python3.7/site-packages/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 

/databricks/python/lib/python3.7/site-packages/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

/databricks/python/lib/python3.7/site-packages/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.7/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    346             parse_int is None and parse_float is None and
    347             parse_constant is None and object_pairs_hook is None and not kw):
--> 348         return _default_decoder.decode(s)
    349     if cls is None:
    350         cls = JSONDecoder

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

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

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

我似乎找不到我为什么会收到此错误的原因,因为该错误以前有效(当我在其中包含带有表情符号的字符串并删除了表情符号时)。

我按照以下步骤删除了表情符号:

string_without_emoji = string.encode('ascii', 'ignore').decode('ascii')

0 个答案:

没有答案