python - JSONDecodeError:期望值:第 1 行第 1 列(字符 0)---Python
问题描述
我尝试按照在 python 中使用谷歌翻译的教程,当我来到代码 whereresult = translator.translate('Mitä sinä teet')
时,我得到了这样的错误:
---------------------------------------------------------------------------
JSONDecodeError Traceback (most recent call last)
<ipython-input-9-7ae6cd50c95e> in <module>
1 from googletrans import Translator
2 translator = Translator()
----> 3 result = translator.translate('Mitä sinä teet')
4 print(result.src)
5 print(result.dest)
~/opt/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.
~/opt/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
~/opt/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
~/opt/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
~/opt/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
~/opt/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):
~/opt/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)
我不知道为什么这个简单的代码不能在我的电脑上运行。我正在使用 mac 和 Python 3。有人可以帮帮我吗?
解决方案
推荐阅读
- mysql - MySQL:如果不存在则迭代插入一条记录
- linux - 如何使命令行参数变量包含标记空格和其他特殊字符的反斜杠?
- android - Android:编写一个在任何地方使用警报对话框的类
- c++ - 如何使用位域结构和联合制作 4 个字节?
- python - python与sqlite3 db一起工作,从db中保存、更新和检索文本
- c# - 我应该在哪里存储网格数据;在静态变量中,它是自己的类,还是一个类中的所有网格类型?
- regex - 匹配 bash 脚本中的单词模式
- python - Pandas groupby:按学期分组
- sql - 在列中显示选择结果
- sql - 仅当班次位于两个日期时,如何获取班次日期一条记录?