Reputation: 91
Okay, I'm going to edit my question.
I am trying to store in a .json file, the prices of products with the corresponding date.
I get the data from another .json file with various products, which are updated every day.
cache_file = get_cache(cache_file)
cache_file is a list with dictionaries, and in each dictionary there is a product with its details.
[{"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": "="}]
Then I get the details of each product, where I want to store the price in another .json file with the current date.
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)
The code stores correctly, but every day it overwrites the results
{"Product 1": [{"12-09-2020": 1169}], "Product 2": [{"12-09-2020": 8925}], "Product 3": [{"12-09-2020": 820}]}
What I need is to store the prices and dates without overwriting
something like this:
{"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}]}
Would you help me to obtain the result I seek? Regards
Upvotes: 0
Views: 140
Reputation: 692
I am trying to simulate your code (see below). And everything is ok with it. Probably something wrong with a file you are reading from or with a method how you process your source data.
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)
Result:
defaultdict(<class 'list'>, {'p1': [{1: 2}, {1: 3}], 'p2': [{1: 2}, {1: 3}]})
Upvotes: 0
Reputation: 23079
As @SauravPathak says, you want to read in the JSON file from the previous run to reconstruct your data structure in memory, add the current set of data to that, and then save the data structure back out to the file. Here's roughly the code you'd need to do that:
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)
Upvotes: 2
Reputation: 1068
Try this
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)
So your result will be stored in this manner
{"Product 1": {"12-09-2020": 1169, "13-09-2020": 1269}, ..}
and you can fetch price of product of a particular date like below
product_prices_date[product_name][date]
Upvotes: 0
Reputation: 836
You'd have to read from json file first, parse it, then append to the parsed dict. And save to json file again.
Upvotes: 1