【发布时间】:2022-01-18 14:22:24
【问题描述】:
我有这个 json 文件,有多个包含 3 个值和 3 个键的字典
[
{
"name": "Mælk",
"co2": 1.25,
"date": 15121999
},
{
"name": "Piskefløde",
"co2": 5.69,
"date": 15071999
},
{
"name": "mælk",
"co2": 10.0,
"date": 10101010
},
{
"name": "milk",
"co2": 745.0,
"date": 19101919
},
{
"name": "M\u00e6lk",
"co2": 12.5,
"date": 10101010
},
{
"name": "Hytteost",
"co2": 44.459999999999994,
"date": 17181717
}
]
到目前为止,我只能弄清楚如何打印“navn”列表 代码:
def remove_prod():
f = open("database.json",)
f = json.load(f)
prod_remove = []
for dct in f:
if "navn" in dct: # the elements are dicts!
prod_remove.append (dct["navn"])
for index, item in enumerate(prod_remove, start=1):
item_title = item.title()
print(f'[{index}] {item_title}')
这使得列表看起来像这样,所以它只是一个带有名称而不是日期的列表
[1] Mælk
[2] Piskefløde
[3] Mælk
[4] Mælk
[5] Mælk
[6] Hytteost
我希望它看起来像这样 - 所以它会在同一行中打印出名称和确切的日期。
[1] Mælk - 15121999 #and so on for the rest
[2] Piskefløde - "with the date from the json file"
[3] Mælk - "with the date from the json file"
[4] Mælk - "with the date from the json file"
[5] Mælk - "with the date from the json file"
[6] Hytteost - "with the date from the json file"
希望你们明白我想做什么,否则请告诉我,我会尽力指定它
【问题讨论】:
-
请更新以提及“name”而不是“navn”。不幸的是,并非所有人都会说非英语。
-
它可以是任何东西,所以我看不出我可以写“hsjadhakdwkj”的问题,它会是一样的吗?
-
我同意,这是一件小事;但是至少对我来说这有点不方便,因为我必须进一步阅读才能理解“navn”这个词与“name”相关联。我的意思是我无法立即做出这种关联,因为我不熟悉这个词。