I'm trying to build a dictionary that should contain data about a coin, date and its close price on the given date.
The data I'm looping through, which I am using as the base out of which I want to build my dict, look like this (it's a Python list):
[('2018-08-31', 'BSV', 0.0), ('2018-08-31', 'EOS', 6.4050002), ('2018-08-31', 'ETC', 12.7299995), ('2018-08-31', 'BNB', 10.96), ('2018-08-31', 'LTC', 62.1300011), ('2018-08-31', 'XMR', 116.1399994), ('2018-08-31', 'BTT', 0.0), ('2018-08-31', 'BCH', 542.4199829), ('2018-08-31', 'ADA', 0.1039), ('2018-08-31', 'DASH', 194.25), ('2018-08-31', 'BTC', 7014.6000977), ('2018-08-31', 'ZEC', 149.6300049), ('2018-08-31', 'XLM', 0.2216), ('2018-08-31', 'XRP', 0.335), ('2018-08-31', 'QTUM', 4.7399998), ('2018-08-31', 'NEO', 20.1100006), ('2018-08-31', 'ETH', 281.6300049), ('2018-08-31', 'TRX', 0.02532), ('2018-09-01', 'BSV', 0.0), ('2018-09-01', 'EOS', 6.6620002), ('2018-09-01', 'ETC', 13.1899996), ('2018-09-01', 'BNB', 11.4499998), ('2018-09-01', 'LTC', 66.5), ('2018-09-01', 'XMR', 121.0299988), ('2018-09-01', 'BTT', 0.0), ('2018-09-01', 'BCH', 618.8699951), ('2018-09-01', 'ADA', 0.1098)]
It's a list of tuples and each tuple has this value in it: (date, coin, close_price)
.
My expected JSON at the end should look like this:
{
"coins": [{
"BTC": [{
"2018-08-31": 7014.6000977
}, {
"2018-09-01": 7197.3999023
}],
"EOS": [{
"2018-08-31": 6.4050002
}, {
"2018-09-01": 6.6620002
}]
}]
}
My current code to build such a JSON looks like this:
prices_json = {"coins": []}
for date, _coin, close_price in prices: #prices is the above-mentioned list of tuples
if date:
prices_json["coins"].append(
{
f"{_coin}": []
}
)
prices_json["coins"][0][f"{_coin}"].append(
{
f"{date}": f"{close_price}"
}
)
The reason I want to build a JSON like this from the provided list of tuples is, that I want to access the close_price data in this way:
my_json_with_prices[f"{coin}"][f"{date}"]
This should result in the close price number, for example 6.4050002
The values of f"{coin}"
and "f{date}"
will be provided in a for loop later on in the script.
If you have any other idea how to access the close_price
values from the list of tuples based on the date
and the coin
, I am open to all of the suggestions.