python - Json 显然被运行 Python 代码破坏了
问题描述
我正在使用Googletrans
用于 python 的 API,谷歌使用可选的目标和源语言规范翻译任何给定的输入字符串。
我的问题:在使用它作为代码的一部分逐行处理多行对话转录后,我似乎破坏了与 Json 相关的内容,现在 API 拒绝运行。
一段示例代码,之前运行完美,但现在抛出以下错误:
from googletrans import Translator
translator = Translator()
translator.translate('안녕하세요.')
---------------------------------------------------------------------------
JSONDecodeError Traceback (most recent call last)
<ipython-input-1-2a9f8e95ca66> in <module>
1 from googletrans import Translator
2 translator = Translator()
----> 3 translator.translate('안녕하세요.')
/usr/local/anaconda3/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.
/usr/local/anaconda3/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
/usr/local/anaconda3/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
/usr/local/anaconda3/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/local/anaconda3/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/local/anaconda3/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/local/anaconda3/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)
可能发生了什么以及可以采取什么措施来解决它?
解决方案
I read the PyPI documentation of googletrans 2.4.0
It mentions the following
The maximum character limit on a single text is 15k.
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).
Probably the text you are translating is too long. Thus, throwing the error.
推荐阅读
- airflow - 在 Airflow 中更改现有 DAG 的开始日期
- reactjs - 如何使用 JEST 和 React 测试库模拟 Geolocation API
- java - 修改后的单元格字体无法保存 (XSSF)
- r - 根据 R 中其他变量的概率(创建 MAR 机制)分配要缺失的变量比例
- python - 为什么 np.hypot 和 np.subtract.outer 与香草广播相比非常快?使用 Numba 并行加速 numpy 进行距离矩阵计算
- web-services - API 网关可以指向 Web 服务 (SOAP),还是可以在微服务中使用 SOAP,以便 API 网关指向它?
- python - 如何修复 .repo/repo/main.py 无效语法问题(Ubuntu 20.04)
- python - 在线镜像
- spring - Querydsl 是否可以防止 SQL 注入
- javascript - 返回不等待if条件