Ethereum: Update the Json Portfolio Does Not Receive Data
As an experienced developer, it is frustrating when a simple script does not work as expected. In this article, we will help the problem with the storage of data from the ethereum portfolio in a json file.
the problem
The Code You Provised Earlier:
`Python
Import json
From Datetime Import Detime
Import requests
The Binance.Client Import Customer
Initializing the Binance API Identification Information
Customer = customer ("your_btc_api_key", "your_btc_api_secret")
Symbol = "Eth"
Exchange = "Binance"
Def update_Portfolio ():
wallet = {}
Get Current Portfolio Data
Answer = customer.get_exchange_data (Exchange = Exchange, Symbol = Symbol)
Data = Json.loads (Answer.text)
Analyze JSON Data to update the portfolio Dictionary
For Assets in Data ["Balances"]:
If asset ["symbol"] == Symbol:
Asset_amount = float (asset ["amount"])
Corne_Name = Asset ["Asset"] ["Name"]
Portfolio [corner_name] = asset_amount
Save updated Portfolio Data in The Json File
With Open ("Portfolio.json", "W") As a file:
Json. Dump (portfolio, file)
UPDATE_PORTFOLIO ()
”
After adding this code, the portfolio.json
File is created in the same directory. However, when you try to update it using json.load ()
or json.dump ()", Nothing Seems to Happen.
The Solution
The Problem Lies in How We Analyze the Json Data of the Binance API. We must access the specific fields of Each Active object that corresponds to the part we want to follow. Here is an updated version of the code:
Python
Import json
From Datetime Import Detime
Import requests
The Binance.Client Import Customer
Initializing the Binance API Identification Information
Customer = customer ("your_btc_api_key", "your_btc_api_secret")
Symbol = "Eth"
Exchange = "Binance"
Def update_Portfolio ():
wallet = {}
Get Current Portfolio Data
Answer = customer.get_exchange_data (Exchange = Exchange, Symbol = Symbol)
Data = Json.loads (Answer.text)
Analyze JSON Data to update the portfolio Dictionary
For Assets in Data ["Balances"]:
If asset ["symbol"] == Symbol:
Asset_amount = float (asset ["amount"])
Corne_Name = Asset ["Asset"] ["Name"]
Portfolio [corner_name] = asset_amount
Save updated Portfolio Data in The Json File
With Open ("Portfolio.json", "W") As a file:
Json. Dump (portfolio, file)
UPDATE_PORTFOLIO ()
”
In This Revised Version:
- We use
json.load ()
Instead or direct accessing the variable `Data '.
- We iter through each Active object in the response and access its attributes (for example,asset [” symbol “]
,
asset [” amount “]) to determine the part we want to follow.
Example of Use Cases
To test this updated code, create a new file called "main.py" with the following content:
Python
Import json
From Datetime Import Detime
Import requests
The Binance.Client Import Customer
Initializing the Binance API Identification Information
Customer = customer ("your_btc_api_key", "your_btc_api_secret")
Symbol = "BTC / USDT"
Exchange = "Binance"
Def Main ():
wallet = {}
Get Current Portfolio Data
Answer = customer.get_exchange_data (Exchange = Exchange, Symbol = Symbol)
Data = Json.loads (Answer.text)
Analyze JSON Data to update the portfolio Dictionary
For Assets in Data ["Balances"]:
If asset ["symbol"] == Symbol:
Asset_amount = float (asset ["amount"])
Corne_Name = Asset ["Asset"] ["Name"]
Portfolio [corner_name] = asset_amount
Save updated Portfolio Data in The Json File
With Open ("Portfolio.json", "W") As a file:
Json. Dump (portfolio, file)
Main ()
”
Run the script using python main.py
. This should create a new portfolio.json
file in the same directory.