我有一个json产品,如下所示,在这个json对象里面,有一个单元名的json数组,这个数组里面有多个对象,但是数量不固定,键和值 已知Json数组命名单元中第一个Json对象的值。这些值 包含在first_keys =[]和first_values中 =[]。
我现在要做的是:例如,要计算第二个json对象的"碳水化合物"值,第二个json对象中的amount值将乘以第一个json对象中的"碳水化合物"值,然后除以第一个json对象中的amount值。3.要计算json对象内部的"蛋白质"值,请将第三个json对象中的"量"变量乘以第一个json对象中的蛋白质值,然后除以第一个json对象内部的"量"变量。
{
"food_id": 0,
"food_name": "NAME",
"food_image": "IMAGE",
"food_kcal": "KCAL",
"food_url": "FOOD_URL",
"food_description": "DESC",
"meal_time": "null",
"food_category": "",
"food_first_unit": "Yemek Kaşığı",
"carb_percent": "72",
"protein_percent": "23",
"fat_percent": "4",
"units": [
{
"unit": "100 Gram",
"amount": "100",
"kcal": "505 kcal",
"carb": "65 g",
"fiber": "3 g",
"protein": "5 g",
"fat": "24 g",
"saturated_fat": "10 g",
"salt": "0.7 g",
"sugar": "34 g"
},
{
"unit": "1 Adet",
"amount": "5",
"kcal": "",
"carb": "",
"fiber": "",
"protein": "",
"fat": "",
"saturated_fat": "",
"salt": "",
"sugar": ""
},
{
"unit": "1 Porsiyon",
"amount": "30",
"kcal": "",
"carb": "",
"fiber": "",
"protein": "",
"fat": "",
"saturated_fat": "",
"salt": "",
"sugar": ""
},
{
"unit": "1 Paket",
"amount": "90",
"kcal": "",
"carb": "",
"fiber": "",
"protein": "",
"fat": "",
"saturated_fat": "",
"salt": "",
"sugar": ""
}
]
}
我的代码:
url = 'www.example.com'
response = requests.get(url)
response.encoding = 'utf-8'
html = response.text
soup = BeautifulSoup(html, 'html.parser')
items = soup.find_all('li')
units = []
first_key = []
first_values = []
names = []
grams = []
for item in items:
name = item.get('data-ntr-srvname-param')
if(str(name) not in "None"):
names.append(str(name).replace("Gramda", "Gram"))
items = soup.find_all("li", {"data-ntr-gram-param": True})
for item in items:
grams.append(float(item["data-ntr-gram-param"]))
for row in soup.select('table[data-ntr-target="facts"] tr'):
name = row.select_one('td:nth-of-type(1)')
value = row.select_one('td:nth-of-type(2)')
if name is not None and value is not None:
first_key.append(name.text.strip())
first_values.append(value.text.strip())
for index, gram in enumerate(grams):
replacedGram=str(gram).replace(".0", "")
units.append({
"unit": f"{names[index]}",
"amount": f"{replacedGram}"
})
data_dict = dict(zip(first_key, first_values))
for index, gram in enumerate(grams):
units[index].update(data_dict)
json_obj = {
"food_id": 0,
"food_name": "NAME",
"food_image": "IMAGE",
"food_kcal": "KCAL",
"food_url": "FOOD_URL",
"food_description": "DESC",
"meal_time": "null",
"food_category":"",
"food_first_unit": "FIRST",
"carb_percent": "72",
"protein_percent": "23",
"fat_percent": "4",
"units": units
}
print("***************")
print(json.dumps(json_obj, indent=4, ensure_ascii=False))
print("***************")
1条答案
按热度按时间x8diyxa71#
为此,我们需要跟踪参考量,并将单位中的值拆分为参考单位。
将
json_obj
更改为data
,下面的代码将获得结果。输出:
我们使用
data["units"][0]
和data["units"][1:]
将引用json从其他数据中分离出来,这仅在至少有1个units
对象时有效。然后为了乘以正确的数值,我们将它们转换为
floats
,并使用.2f
的f-string方法将值舍入到两位小数(使其再次成为字符串)。如果我们在这里不使用舍入,我们将遇到浮点精度错误。