Python 获取json.decoder.JSONDecodeError:预期值:第1行第1列
代码: 插入的打印语句的输出:Python 获取json.decoder.JSONDecodeError:预期值:第1行第1列,python,json,python-3.x,Python,Json,Python 3.x,代码: 插入的打印语句的输出: def get_result(response_path): response_s3_path = re.sub(config.path_prefix,'',response_path).strip('/') print('== response_s3_path') print(response_s3_path) response_local_path = download_file_from_s3(response_s3_path) prin
def get_result(response_path):
response_s3_path = re.sub(config.path_prefix,'',response_path).strip('/')
print('== response_s3_path')
print(response_s3_path)
response_local_path = download_file_from_s3(response_s3_path)
print('== response_local_path')
print(response_local_path)
if response_local_path==-1:
print('Failed to open response_local path')
return -1
with open(response_local_path, 'r') as f:
print(type(json.load(f)))
print(json.load(f))
if isinstance(json.load(f), str):
response = json.loads(json.load(f))
#response = json.load(f)
elif isinstance(json.load(f), dict):
print('json.loads(dict)')
response = json.load(f)
未能理解如果json.load(f)具有类“dict”,并且在打印时,如果它似乎是一个有效的字典,那么为什么它会在行中给我JsonDecodeError:
elif-isinstance(json.load(f),dict):
请帮助。您正在多次执行
json.load(f)
f
这里有一个文件对象,它可以记住它的读取位置。加载JSON一次后,文件指针将位于文件末尾。再次尝试读取将导致它不返回任何值,除非您将其倒带,从而导致显示错误
不要多次加载该文件,这也是非常浪费的。只做一次:
File "/Users/AjayB/anaconda3/envs/MyDjangoEnv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/Users/AjayB/anaconda3/envs/MyDjangoEnv/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/AjayB/anaconda3/envs/MyDjangoEnv/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/AjayB/anaconda3/envs/MyDjangoEnv/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/Users/AjayB/Desktop/Python/crackd/misc/training_apis/training_apis_server/Apis/views.py", line 1141, in get_inference_results_api
_, inference_array = get_inference_results_from_df(df_pass)
File "/Users/AjayB/Desktop/Python/crackd/misc/training_apis/training_apis_server/Apis/views.py", line 1260, in get_inference_results_from_df
inference_array = get_inference_array(inference_links)
File "/Users/AjayB/Desktop/Python/crackd/misc/training_apis/training_apis_server/Apis/views.py", line 721, in get_inference_array
final_response_params = get_result(obj_tmp['response']['path'])
File "/Users/AjayB/Desktop/Python/crackd/misc/training_apis/training_apis_server/Apis/views.py", line 644, in get_result
print(isinstance(json.load(f), dict))
File "/Users/AjayB/anaconda3/envs/MyDjangoEnv/lib/python3.6/json/__init__.py", line 299, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/Users/AjayB/anaconda3/envs/MyDjangoEnv/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/Users/AjayB/anaconda3/envs/MyDjangoEnv/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Users/AjayB/anaconda3/envs/MyDjangoEnv/lib/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
[08/Nov/2019 05:49:38] "POST /list/inference_history/ HTTP/1.1" 500 147054
从现在起,使用
数据而不是重复json.load(f)
是的,这对我很有效
with open(response_local_path, 'r') as f:
data = json.load(f)
print(data)
不再多次使用json.load(f)。您试图多次读取同一文件指针。你只能读一次;除非在多次读取之间倒带。不要尝试多次解码JSON!这也很浪费,这很有帮助。我不知道他的名字。我不会忘记的,非常感谢。:)只要不再需要f
块,就应该将与
块分开,这样它就可以关闭。
with open(response_local_path, 'r') as f:
data = json.load(f)
print(data)
with open(response_local_path, 'r') as f:
resp = json.load(f)
if isinstance(resp, str):
response = json.loads(resp)
elif isinstance(resp, dict):
response = resp
else:
print(type(resp))