python 中解决 raise jsondecodeerror(expecting value, s, err.value) from none
在 python 中使用 url 和 api 时,您通常必须使用 urllib 和 json 库。 更重要的是,json 库有助于处理 json 数据,这是传输数据的默认方式,尤其是使用 api 时。
在 json 库中,有一个方法,loads(),它返回 jsondecodeerror 错误。 在本文中,我们将讨论如何解决此类错误并进行适当的处理。
从 python 中使用 try 的 none 中解决 raise jsondecodeerror("expecting value", s, err.value)
在处理 json 之前,我们经常必须通过 urllib 包接收数据。 但是,在使用 urllib 包时,了解如何将此类包导入代码中非常重要,因为这可能会导致错误。
为了使用 urllib 包,我们必须导入它。 通常,人们可能会按如下方式导入它。
import urllib
querystring = { 'name' : 'jhon', 'age' : '18'}
urllib.parse.urlencode(querystring)
上面代码的输出将给出一个 attributeerror:
traceback (most recent call last):
file "c:\users\akinl\documents\html\python\test.py", line 3, in
urllib.parse.urlencode(querystring)
attributeerror: module 'urllib' has no attribute 'parse'
导入urllib的正确方法如下所示。
import urllib.parse
querystring = {'name': 'jhon', 'age': '18'}
urllib.parse.urlencode(querystring)
或者,您可以使用 as
关键字和别名(通常更短)来更轻松地编写 python 代码。
import urllib.parse as urlp
querystring = {'name': 'jhon', 'age': '18'}
urlp.urlencode(querystring)
以上所有内容都适用于请求、错误和 robotsparser:
import urllib.request
import urllib.error
import urllib.robotparser
解决了这些常见错误后,我们可以进一步处理在处理引发 jsondecodeerror 错误的 url 时经常与 urllib 库一起使用的函数,如前所述,即 json.load()
函数。
raise jsondecodeerror("expecting value", s, err.value) from none
json.decoder.jsondecodeerror: expecting value: line 1 column 1 (char 0)
load()
方法解析作为参数接收的有效 json 字符串,并将其转换为 python 字典以进行操作。 错误消息显示它需要一个 json 值,但没有收到。
这意味着您的代码没有解析 json 字符串或将空字符串解析到 load()
方法。 快速的代码片段可以轻松验证这一点。
import json
data = ""
js = json.loads(data)
代码的输出:
traceback (most recent call last):
file "c:\users\akinl\documents\python\texts.py", line 4, in
js = json.loads(data)
file "c:\python310\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
file "c:\python310\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
file "c:\python310\lib\json\decoder.py", line 355, in raw_decode
raise jsondecodeerror("expecting value", s, err.value) from none
json.decoder.jsondecodeerror: expecting value: line 1 column 1 (char 0)
存在相同的错误消息,我们可以确定错误来自空字符串参数。
对于更详细的示例,让我们尝试访问 google map api 并收集用户位置,例如 us 或 ng,但它不会返回任何值。
import urllib.parse
import urllib.request
import json
googleurl = 'http://maps.googleapis.com/maps/api/geocode/json?'
while true:
address = input('enter location: ')
if address == "exit":
break
if len(address) < 1:
break
url = googleurl urllib.parse.urlencode({'sensor': 'false',
'address': address})
print('retrieving', url)
ureq = urllib.request.urlopen(url)
data = ureq.read()
print('returned', len(data), 'characters')
js = json.loads(data)
print(js)
代码的输出:
traceback (most recent call last):
file "c:\users\akinl\documents\html\python\jsonart.py", line 18, in
js = json.loads(str(data))
file "c:\python310\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
file "c:\python310\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
file "c:\python310\lib\json\decoder.py", line 355, in raw_decode
raise jsondecodeerror("expecting value", s, err.value) from none
json.decoder.jsondecodeerror: expecting value: line 1 column 1 (char 0)
我们得到了同样的错误。 然而,为了捕获此类错误并防止崩溃,我们可以使用 try/ except 逻辑来保护我们的代码。
因此,如果 api 在请求时未返回任何 json 值,我们可以返回另一个表达式而不是错误。
上面的代码就变成:
import urllib.parse
import urllib.request
import json
googleurl = 'http://maps.googleapis.com/maps/api/geocode/json?'
while true:
address = input('enter location: ')
if address == "exit":
break
if len(address) < 1:
break
url = googleurl urllib.parse.urlencode({'sensor': 'false',
'address': address})
print('retrieving', url)
ureq = urllib.request.urlopen(url)
data = ureq.read()
print('returned', len(data), 'characters')
try:
js = json.loads(str(data))
except:
print("no json returned")
当我们输入位置为 us 时代码的输出:
enter location: us
retrieving http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=us
returned 237 characters
no json returned
由于没有返回 json 值,因此代码打印 no json returned。 因为错误更多的是无法控制的不存在参数,所以使用 try/ except
很重要。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
发布时间:2023/07/09 浏览次数:173 分类:python
-
我们将通过示例介绍python中何时出现 address already in use 错误以及如何解决。python 中的错误address already in use 本文将讲述运行使用端口的程序时发生的python堆栈错误。
发布时间:2023/07/09 浏览次数:607 分类:python
-
在本篇文章中,我们的目标是探索解决 python 中的 valueerror: math domain error 错误的不同方法。当编码方面数学(基础或高级)的使用存在固有缺陷时,python 中通常会引发 valueerror: math domain error 错
python 错误 name xrange is not defined
发布时间:2023/07/09 浏览次数:153 分类:python
-
本篇文章将介绍如何解决 python 中 name 'xrange' is not defined 的错误。解决python中name 'xrange' is not defined错误 让我们尝试理解为什么会发生这个特定的错误。 让我们首先尝试复制这个问题。
python 错误 typeerror: list indices must be integers, not str
发布时间:2023/07/09 浏览次数:954 分类:python
-
在本篇文章中,我们的目标是探索如何避免 typeerror: list indices must be integers or slices, not str。typeerror主要发生在python中,每当操作的数据类型出现问题时。
发布时间:2023/07/09 浏览次数:2241 分类:python
-
在 python 中,attributeerror 是在未定义 __enter__ 函数的情况下通过 with 语句使用类的对象时导致的错误。
python 错误 modulenotfounderror: no module named '_ctypes'
发布时间:2023/07/09 浏览次数:686 分类:python
-
本篇文章旨在了解如何解决 python 中的 modulenotfounderror: no module named '_ctypes'。了解python中 modulenotfounderror: no module named '_ctypes' 根本原因
发布时间:2023/07/09 浏览次数:1063 分类:python
-
本篇文章将介绍如何修复 python 中的 attributeerror: '_io.textiowrapper' object has no attribute 'split'。在 _io.textiowrapper 上使用 split() 方法会返回 attributeerror
python 错误 attributeerror: _csv.reader object has no attribute next
发布时间:2023/07/09 浏览次数:286 分类:python
-
本篇文章将介绍如何修复 python 中的 attributeerror: '_csv.reader' object has no attribute 'next'。修复 python 中的 attributeerror: '_csv.reader' object has no attribute 'next' 错误
python 错误 error: invalid command bdist_wheel
发布时间:2023/07/09 浏览次数:847 分类:python
-
在 python 中构建 wheel 时,有时 setup.py 可能会退出并出现错误 invalid command 'bdist_wheel'。 本篇文章将讨论在 python 中解决此问题的可能ag捕鱼王app官网的解决方案。安装wheel包来修复python中 error:invalid command 'bdist_