亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何向字典鍵添加多個值

如何向字典鍵添加多個值

月關寶盒 2023-08-03 16:38:38
好的,我要編輯我的問題。我試圖將相應日期的產品價格存儲在 .json 文件中。我從另一個包含各種產品的 .json 文件獲取數據,這些產品每天都會更新。cache_file = get_cache(cache_file)cache_file 是一個包含字典的列表,每個字典中都有一個產品及其詳細信息。[{"title": "Product 1", "price": 11695000, "img": "img-link", "link": "link-to-product", "estado_precio": "="}, {"title": "Product 2", "price": 8925000, "img": "img-link", "link": "link-to-product", "estado_precio": "="}, {"title": "Product 3", "price": 8200000, "img": "img- link", "link": "link-to-product", "estado_precio": "="}]然后,我獲取每個產品的詳細信息,我想將價格存儲在另一個具有當前日期的 .json 文件中。product_prices_date = defaultdict(list)for products_details in cache_file:                           prices_date = {}    prices_date[fecha] = products_details['price']         product_prices_date[products_details['title']].append(prices_date)            save_to_cache(product_prices_date, cache_file)代碼存儲正確,但每天都會覆蓋結果{"Product 1": [{"12-09-2020": 1169}], "Product 2": [{"12-09-2020": 8925}], "Product 3": [{"12-09-2020": 820}]}我需要的是存儲價格和日期而不覆蓋像這樣的東西:{"Product 1": [{"12-09-2020": 1169}, {"13-09-2020": 1269}], "Product 2": [{"12-09-2020": 8925}, {"13-09-2020": 8925}], "Product 3": [{"12-09-2020": 820}, {"13-09-2020": 850}]}你能幫助我獲得我想要的結果嗎?問候
查看完整描述

4 回答

?
守著星空守著你

TA貢獻1799條經驗 獲得超8個贊

您想要從上次運行中讀取 JSON 文件,以在內存中重建數據結構,將當前的數據集添加到其中,然后將數據結構保存回文件中。以下是您需要執行此操作的大致代碼:


import json

import os


output_path = '/tmp/report.json'


def add_daily_vaules(file):


    # Read in existing data file if it exists, else start from empty dict

    if os.path.exists(output_path):

        with open(output_path) as f:

            product_prices_date = json.load(f)

    else:

        product_prices_date = {}


    # Add each of today's products to the data

    for products_details in file:

        title = products_details['title']

        price = products_details['price']

        date = products_details['date']

        # This is the key - you want to append to a prior entry for a specific

        # title if it already exists in the data, else you want to first add

        # an empty list to the data so that you can append either way

        if title in product_prices_date:

            prices_date = product_prices_date[title]

        else:

            prices_date = []

            product_prices_date[title] = prices_date

        prices_date.append({date:price})


    # Save the structure out to the JSON file

    with open(output_path, "w") as f:

        json.dump(f, product_prices_date)


查看完整回答
反對 回復 2023-08-03
?
米脂

TA貢獻1836條經驗 獲得超3個贊

您必須首先從 json 文件中讀取,解析它,然后附加到解析后的字典中。并再次保存到json文件。



查看完整回答
反對 回復 2023-08-03
?
神不在的星期二

TA貢獻1963條經驗 獲得超6個贊

我正在嘗試模擬您的代碼(見下文)。一切都很好。您正在讀取的文件或處理源數據的方法可能有問題。


from collections import defaultdict

product_prices_date = defaultdict(list)

prices_date = {}

prices_date = {1:2}

product_prices_date['p1'].append(prices_date)

prices_date = {}

prices_date = {1:3}

product_prices_date['p1'].append(prices_date)

prices_date = {}

prices_date = {1:2}

product_prices_date['p2'].append(prices_date)

prices_date = {}

prices_date = {1:3}

product_prices_date['p2'].append(prices_date)

print(product_prices_date)

結果:


defaultdict(<class 'list'>, {'p1': [{1: 2}, {1: 3}], 'p2': [{1: 2}, {1: 3}]})


查看完整回答
反對 回復 2023-08-03
?
慕村225694

TA貢獻1880條經驗 獲得超4個贊

嘗試這個


product_prices_date = defaultdict(dict)

for products_details in file:  

                              

    product_prices_date[product_name].update({todays_date: products_details['price']})

            

save_to_cache(product_prices_date, cache_file)

所以你的結果將以這種方式存儲


{"Product 1": {"12-09-2020": 1169, "13-09-2020": 1269}, ..}

您可以獲取特定日期的產品價格,如下所示


product_prices_date[product_name][date]


查看完整回答
反對 回復 2023-08-03
  • 4 回答
  • 0 關注
  • 224 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號