Python requests.get return 错误 json.decoder.JSONDecodeError
Python requests.get return error json.decoder.JSONDecodeError
我正在尝试从我无法控制的服务器读取批量数据。
错误:
json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 1794)
Error Image here
此 .json()
调用引发错误 json.decoder.JSONDecodeError json.decoder
。
import requests
Data = requests.get(Data_Url ,headers=session["headers"]).json()
print(Data)
这个 .text
returns 字符串中的数据我无法操作。
import requests
Data = requests.get(Data_Url ,headers=session["headers"]).text
print(Data)
如下图数据格式好像是
{}
{}
{}
如何操作 request.get
响应以便我具有 JSON 格式并由 {},{}
分隔?
{ "Data" : [{},{}]}
{"resourceType":"Person","id":"cg3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"3g3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"pg3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"GA3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"zQ3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"qQ3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"Fw3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"Nw3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"hw3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"DSQ3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
您从 .text
获得的结果不是有效的 JSON(或 Python 文字)格式。研究结果后,我确定返回的字符串中的每一行末尾都缺少可以纠正该问题的字符“}]}]}
”。
下面的代码将它们添加到每一行,然后 parses/evaluated 使用 ast.literal_eval()
function to turn it into a Python dictionary. A list comprehension 将它们放入列表中。换句话说,您不必像您建议的 {"Data": [{}, {}, ...]}
那样费心将它们嵌套在字典中(除非您 确实 出于某种未知原因想要这么做)。
from ast import literal_eval
import json
from pprint import pprint
requests_get_text = """\
{"resourceType":"Person","id":"cg3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"3g3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"pg3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"GA3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"zQ3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"qQ3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"Fw3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"Nw3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"hw3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"DSQ3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
"""
# Convert result from requests.get().text into valid JSON/Python format.
data = [literal_eval(f'{line}' '}]}]}\n')
for line in requests_get_text.splitlines()]
pprint(data, sort_dicts=False)
输出:
[{'resourceType': 'Person',
'id': 'cg3',
'extension': [{'extension': [{'valueCoding': {'system': 'http://terminology.hl7.org/',
'code': 'UNK',
'display': 'Unknown'}}]}]},
{'resourceType': 'Person',
'id': '3g3',
'extension': [{'extension': [{'valueCoding': {'system': 'http://terminology.hl7.org/',
'code': 'UNK',
'display': 'Unknown'}}]}]},
{'resourceType': 'Person',
'id': 'pg3',
'extension': [{'extension': [{'valueCoding': {'system': 'http://terminology.hl7.org/',
'code': 'UNK',
'display': 'Unknown'}}]}]},
{'resourceType': 'Person',
'id': 'GA3',
'extension': [{'extension': [{'valueCoding': {'system': 'http://terminology.hl7.org/',
'code': 'UNK',
'display': 'Unknown'}}]}]},
{'resourceType': 'Person',
'id': 'zQ3',
'extension': [{'extension': [{'valueCoding': {'system': 'http://terminology.hl7.org/',
'code': 'UNK',
'display': 'Unknown'}}]}]},
{'resourceType': 'Person',
'id': 'qQ3',
'extension': [{'extension': [{'valueCoding': {'system': 'http://terminology.hl7.org/',
'code': 'UNK',
'display': 'Unknown'}}]}]},
{'resourceType': 'Person',
'id': 'Fw3',
'extension': [{'extension': [{'valueCoding': {'system': 'http://terminology.hl7.org/',
'code': 'UNK',
'display': 'Unknown'}}]}]},
{'resourceType': 'Person',
'id': 'Nw3',
'extension': [{'extension': [{'valueCoding': {'system': 'http://terminology.hl7.org/',
'code': 'UNK',
'display': 'Unknown'}}]}]},
{'resourceType': 'Person',
'id': 'hw3',
'extension': [{'extension': [{'valueCoding': {'system': 'http://terminology.hl7.org/',
'code': 'UNK',
'display': 'Unknown'}}]}]},
{'resourceType': 'Person',
'id': 'DSQ3',
'extension': [{'extension': [{'valueCoding': {'system': 'http://terminology.hl7.org/',
'code': 'UNK',
'display': 'Unknown'}}]}]}]
我正在尝试从我无法控制的服务器读取批量数据。
错误:
json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 1794)
Error Image here
此 .json()
调用引发错误 json.decoder.JSONDecodeError json.decoder
。
import requests
Data = requests.get(Data_Url ,headers=session["headers"]).json()
print(Data)
这个 .text
returns 字符串中的数据我无法操作。
import requests
Data = requests.get(Data_Url ,headers=session["headers"]).text
print(Data)
如下图数据格式好像是
{}
{}
{}
如何操作 request.get
响应以便我具有 JSON 格式并由 {},{}
分隔?
{ "Data" : [{},{}]}
{"resourceType":"Person","id":"cg3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"3g3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"pg3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"GA3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"zQ3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"qQ3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"Fw3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"Nw3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"hw3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"DSQ3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
您从 .text
获得的结果不是有效的 JSON(或 Python 文字)格式。研究结果后,我确定返回的字符串中的每一行末尾都缺少可以纠正该问题的字符“}]}]}
”。
下面的代码将它们添加到每一行,然后 parses/evaluated 使用 ast.literal_eval()
function to turn it into a Python dictionary. A list comprehension 将它们放入列表中。换句话说,您不必像您建议的 {"Data": [{}, {}, ...]}
那样费心将它们嵌套在字典中(除非您 确实 出于某种未知原因想要这么做)。
from ast import literal_eval
import json
from pprint import pprint
requests_get_text = """\
{"resourceType":"Person","id":"cg3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"3g3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"pg3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"GA3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"zQ3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"qQ3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"Fw3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"Nw3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"hw3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
{"resourceType":"Person","id":"DSQ3","extension":[{"extension":[{"valueCoding":{"system":"http://terminology.hl7.org/","code":"UNK","display":"Unknown"}
"""
# Convert result from requests.get().text into valid JSON/Python format.
data = [literal_eval(f'{line}' '}]}]}\n')
for line in requests_get_text.splitlines()]
pprint(data, sort_dicts=False)
输出:
[{'resourceType': 'Person',
'id': 'cg3',
'extension': [{'extension': [{'valueCoding': {'system': 'http://terminology.hl7.org/',
'code': 'UNK',
'display': 'Unknown'}}]}]},
{'resourceType': 'Person',
'id': '3g3',
'extension': [{'extension': [{'valueCoding': {'system': 'http://terminology.hl7.org/',
'code': 'UNK',
'display': 'Unknown'}}]}]},
{'resourceType': 'Person',
'id': 'pg3',
'extension': [{'extension': [{'valueCoding': {'system': 'http://terminology.hl7.org/',
'code': 'UNK',
'display': 'Unknown'}}]}]},
{'resourceType': 'Person',
'id': 'GA3',
'extension': [{'extension': [{'valueCoding': {'system': 'http://terminology.hl7.org/',
'code': 'UNK',
'display': 'Unknown'}}]}]},
{'resourceType': 'Person',
'id': 'zQ3',
'extension': [{'extension': [{'valueCoding': {'system': 'http://terminology.hl7.org/',
'code': 'UNK',
'display': 'Unknown'}}]}]},
{'resourceType': 'Person',
'id': 'qQ3',
'extension': [{'extension': [{'valueCoding': {'system': 'http://terminology.hl7.org/',
'code': 'UNK',
'display': 'Unknown'}}]}]},
{'resourceType': 'Person',
'id': 'Fw3',
'extension': [{'extension': [{'valueCoding': {'system': 'http://terminology.hl7.org/',
'code': 'UNK',
'display': 'Unknown'}}]}]},
{'resourceType': 'Person',
'id': 'Nw3',
'extension': [{'extension': [{'valueCoding': {'system': 'http://terminology.hl7.org/',
'code': 'UNK',
'display': 'Unknown'}}]}]},
{'resourceType': 'Person',
'id': 'hw3',
'extension': [{'extension': [{'valueCoding': {'system': 'http://terminology.hl7.org/',
'code': 'UNK',
'display': 'Unknown'}}]}]},
{'resourceType': 'Person',
'id': 'DSQ3',
'extension': [{'extension': [{'valueCoding': {'system': 'http://terminology.hl7.org/',
'code': 'UNK',
'display': 'Unknown'}}]}]}]