我有一个带有动态字段的json数组,其中一些数组没有包含所有字段。 例如:
[
{
"Name": "AFG LIMITED",
"Vendor ID": "008343",
"EGID": "67888",
"FID": "83748374"
},
{
"Name": "ABC LIMITED",
"Vendor ID": "008333",
"EGID": "67888",
"AID": "0000292"
"FID": "98979"
},
]
我需要提取带有标题的特定键&;管道分隔符,如:Name | Vendor ID | EGID | AID(仅存在于第二个数组中)。如果任何键不存在,则其值应为null 我试着用下面的代码来解析这个,但是它在第二行中断了,因为援助丢失了
import json
with open("sample.json", "r") as rf:
decoded_data = json.load(rf)
# Check is the json object was loaded correctly
try:
for i in decoded_data:
print i["Name"],"|",i["Vendor ID"]"|",i["EGID"],"|",i["AId"]
except KeyError:
print(null)
上述代码的输出: AFG有限公司| 008343 | 67888 |空
目前没有回答
相关问题 更多 >
编程相关推荐