Python 如果要写入Json文件的数据结构不正确,该怎么办?
我试图使用API和Python检索数据,然后将其保存在Json文件中 不幸的是,API返回的数据结构非常糟糕,因此我无法以编程方式搜索该文件 然后,数据如下所示:Python 如果要写入Json文件的数据结构不正确,该怎么办?,python,json,api,Python,Json,Api,我试图使用API和Python检索数据,然后将其保存在Json文件中 不幸的是,API返回的数据结构非常糟糕,因此我无法以编程方式搜索该文件 然后,数据如下所示: { "data": [ [ [], null ], [ [ { "1. symbol": "YI", "2. name": "
{
"data": [
[
[],
null
],
[
[
{
"1. symbol": "YI",
"2. name": "111 Inc.",
"3. type": "Equity",
"4. region": "United States",
"5. marketOpen": "09:30",
"6. marketClose": "16:00",
"7. timezone": "UTC-05",
"8. currency": "USD",
"9. matchScore": "0.3636"
}
],
null
],
[
[
{
"1. symbol": "2GB.DEX",
"2. name": "2G Energy AG",
"3. type": "Equity",
"4. region": "XETRA",
"5. marketOpen": "08:00",
"6. marketClose": "20:00",
"7. timezone": "UTC+02",
"8. currency": "EUR",
"9. matchScore": "0.5714"
},
{
"1. symbol": "2GL.FRK",
"2. name": "Gaming and Leisure Properties Inc.",
"3. type": "Equity",
"4. region": "Frankfurt",
"5. marketOpen": "08:00",
"6. marketClose": "20:00",
"7. timezone": "UTC+02",
"8. currency": "EUR",
"9. matchScore": "0.5714"
}
],
null
]
]
}
我设法在保存之前过滤掉None,但括号保留并破坏了结构
有人知道我怎样才能使这些数据结构良好吗?我使用的是Python 3.8嗯,这里肯定有一个模式。 基本上,数据结构如下:
{
"data":[
[[<values you look for>], null],
[[<values you look for>], null]
]
}
您将得到一个简单的dict列表。您的json结构非常好,只需使用
json
库正确处理数据即可:
import json
data = """{
"data": [
[
[],
null
],
[
[
{
"1. symbol": "YI",
"2. name": "111 Inc.",
"3. type": "Equity",
"4. region": "United States",
"5. marketOpen": "09:30",
"6. marketClose": "16:00",
"7. timezone": "UTC-05",
"8. currency": "USD",
"9. matchScore": "0.3636"
}
],
null
],
[
[
{
"1. symbol": "2GB.DEX",
"2. name": "2G Energy AG",
"3. type": "Equity",
"4. region": "XETRA",
"5. marketOpen": "08:00",
"6. marketClose": "20:00",
"7. timezone": "UTC+02",
"8. currency": "EUR",
"9. matchScore": "0.5714"
},
{
"1. symbol": "2GL.FRK",
"2. name": "Gaming and Leisure Properties Inc.",
"3. type": "Equity",
"4. region": "Frankfurt",
"5. marketOpen": "08:00",
"6. marketClose": "20:00",
"7. timezone": "UTC+02",
"8. currency": "EUR",
"9. matchScore": "0.5714"
}
],
null
]
]
}"""
data_json = json.loads(data)
输出
{'data': [[[], None],
[[{'1. symbol': 'YI',
'2. name': '111 Inc.',
'3. type': 'Equity',
'4. region': 'United States',
'5. marketOpen': '09:30',
'6. marketClose': '16:00',
'7. timezone': 'UTC-05',
'8. currency': 'USD',
'9. matchScore': '0.3636'}],
None],
[[{'1. symbol': '2GB.DEX',
'2. name': '2G Energy AG',
'3. type': 'Equity',
'4. region': 'XETRA',
'5. marketOpen': '08:00',
'6. marketClose': '20:00',
'7. timezone': 'UTC+02',
'8. currency': 'EUR',
'9. matchScore': '0.5714'},
{'1. symbol': '2GL.FRK',
'2. name': 'Gaming and Leisure Properties Inc.',
'3. type': 'Equity',
'4. region': 'Frankfurt',
'5. marketOpen': '08:00',
'6. marketClose': '20:00',
'7. timezone': 'UTC+02',
'8. currency': 'EUR',
'9. matchScore': '0.5714'}],
None]]}
你认为什么是“结构良好”?您尝试过将数据获取到所需的输出吗?我不知道为什么会出现空值,这是我的问题。我没有看到图案,所以谢谢你,先生:)
{'data': [[[], None],
[[{'1. symbol': 'YI',
'2. name': '111 Inc.',
'3. type': 'Equity',
'4. region': 'United States',
'5. marketOpen': '09:30',
'6. marketClose': '16:00',
'7. timezone': 'UTC-05',
'8. currency': 'USD',
'9. matchScore': '0.3636'}],
None],
[[{'1. symbol': '2GB.DEX',
'2. name': '2G Energy AG',
'3. type': 'Equity',
'4. region': 'XETRA',
'5. marketOpen': '08:00',
'6. marketClose': '20:00',
'7. timezone': 'UTC+02',
'8. currency': 'EUR',
'9. matchScore': '0.5714'},
{'1. symbol': '2GL.FRK',
'2. name': 'Gaming and Leisure Properties Inc.',
'3. type': 'Equity',
'4. region': 'Frankfurt',
'5. marketOpen': '08:00',
'6. marketClose': '20:00',
'7. timezone': 'UTC+02',
'8. currency': 'EUR',
'9. matchScore': '0.5714'}],
None]]}