一段時間以來,我一直在搞亂 JSON,只是將其作為文本推出,并沒有傷害任何人(據我所知),但我想開始正確地做事。
這是我的代碼:
term=temp['specifications']['PKG&HAZMAT']
for j in term:
try:
got=j['value']
except:
pass
print(got)
這是我的 json 檔案:
"specifications": {
"PKG&HAZMAT": [{
"value": "FLETP",
"name": "VMRS",
"key": "a8f1W000000fxho"
},
{
"value": "EA",
"name": "Sales Unit",
"key": "a8f1W000000fxhv"
},
{
"value": "0",
"name": "Quantity per Application",
"key": "a8f1W000000fxhy"
},
{
"value": "5.8",
"name": "Height Each",
"key": "a8f1W000000fxi2"
},
{
"value": "20.35",
"name": "Width Each",
"key": "a8f1W000000fxi3"
},
{
"value": "18.95",
"name": "Length Each",
"key": "a8f1W000000fxi4"
},
{
"value": "14.47",
"name": "Weight Each",
"key": "a8f1W000000fxi5"
},
{
"value": "WARNING Cancer and Reproductive Harm - www.P65Warnings.ca.gov",
"name": "Prop 65 Statement",
"key": "a8f1W000000g3EN"
}
],
"MARKETING": [{
"value": "Spiral wound",
"name": "Benefit 1",
"key": "a8f1W000000TOAF"
},
{
"value": "Includes hang collar",
"name": "Benefit 2",
"key": "a8f1W000000TOAG"
},
{
"value": "One bundle for easy management",
"name": "Benefit 3",
"key": "a8f1W000000TOAH"
}
],
"PROP65": [{
"value": "1GENERAL",
"name": "Code",
"key": "a8f6S000000btYS"
},
{
"value": "WARNING: Cancer and Reproductive Harm - www.P65Warnings.ca.gov.",
"name": "Short Warning",
"key": "a8f6S000000btYT"
}
],
"FP_PartType_F552": [{
"value": "15",
"name": "Length",
"key": "a8f6S000000Ynnr"
},
{
"value": "ABS with zinc die cast plugs",
"name": "Electric Cable Type",
"key": "a8f6S000000YnYr"
}
]
},
我的輸出是這些:
FLETP
EA
0
5.8
20.35
18.95
14.47
如果您在我想提取的 JSON 檔案中看到我想要提取的 JSON 檔案name和JSON 檔案中的圖片,我想要這些格式的輸出,value請讓我知道
預期輸出:

uj5u.com熱心網友回復:
有兩種方法可以做到這一點:
- 您可以使用
pandas將其放入資料框/表中(使用.json_normalize() - 只需使用 for 回圈
給定資料:
data = {"specifications":
{
"PKG&HAZMAT": [{
"value": "FLETP",
"name": "VMRS",
"key": "a8f1W000000fxho"
},
{
"value": "EA",
"name": "Sales Unit",
"key": "a8f1W000000fxhv"
},
{
"value": "0",
"name": "Quantity per Application",
"key": "a8f1W000000fxhy"
},
{
"value": "5.8",
"name": "Height Each",
"key": "a8f1W000000fxi2"
},
{
"value": "20.35",
"name": "Width Each",
"key": "a8f1W000000fxi3"
},
{
"value": "18.95",
"name": "Length Each",
"key": "a8f1W000000fxi4"
},
{
"value": "14.47",
"name": "Weight Each",
"key": "a8f1W000000fxi5"
},
{
"value": "WARNING Cancer and Reproductive Harm - www.P65Warnings.ca.gov",
"name": "Prop 65 Statement",
"key": "a8f1W000000g3EN"
}
],
"MARKETING": [{
"value": "Spiral wound",
"name": "Benefit 1",
"key": "a8f1W000000TOAF"
},
{
"value": "Includes hang collar",
"name": "Benefit 2",
"key": "a8f1W000000TOAG"
},
{
"value": "One bundle for easy management",
"name": "Benefit 3",
"key": "a8f1W000000TOAH"
}
],
"PROP65": [{
"value": "1GENERAL",
"name": "Code",
"key": "a8f6S000000btYS"
},
{
"value": "WARNING: Cancer and Reproductive Harm - www.P65Warnings.ca.gov.",
"name": "Short Warning",
"key": "a8f6S000000btYT"
}
],
"FP_PartType_F552": [{
"value": "15",
"name": "Length",
"key": "a8f6S000000Ynnr"
},
{
"value": "ABS with zinc die cast plugs",
"name": "Electric Cable Type",
"key": "a8f6S000000YnYr"
}
]
}}
代碼 1:
import pandas as pd
term = data['specifications']['PKG&HAZMAT']
df = pd.json_normalize(term)[['name','value']]
輸出:
print(df)
name value
0 VMRS FLETP
1 Sales Unit EA
2 Quantity per Application 0
3 Height Each 5.8
4 Width Each 20.35
5 Length Each 18.95
6 Weight Each 14.47
7 Prop 65 Statement WARNING Cancer and Reproductive Harm - www.P65...
代碼 2:
term = data['specifications']['PKG&HAZMAT']
for j in term:
name = j['name']
value = j['value']
print(name, value)
輸出:
VMRS FLETP
Sales Unit EA
Quantity per Application 0
Height Each 5.8
Width Each 20.35
Length Each 18.95
Weight Each 14.47
Prop 65 Statement WARNING Cancer and Reproductive Harm - www.P65Warnings.ca.gov
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/486875.html
上一篇:如何使用beautifulsoup requests縮小刮擦的結果?
下一篇:使用selenium抓取資料
