'Save API filtered data in Excel file
I have a problem with some data from Binance API. What I want is to keep a list of the USDT paired coins from binance. The problem is Binance give me a complete list of all kind paired coins, and I'm unable to filter only the USDT paired.
I need to save in Excel file only the USDT paired coins. I have write the code to keep all the coin list:
import requests, json
data=requests.get('https://api.binance.com' + '/api/v1/ticker/allBookTickers')
data=json.loads(data.content)
print(data)
Solution 1:[1]
What you need is just pandas module. You can try the code below:
import requests
import json
import pandas as pd
data=requests.get('https://api.binance.com' + '/api/v1/ticker/allBookTickers')
data=json.loads(data.content)
dataframe = pd.DataFrame(data)
dataframe.to_excel("my_data.xlsx")
Your file will be saved in the same directory as the script and is named my_data.xlsx
Note that the dataframe variable is something like what follows:
| symbol | bidPrice | bidQty | askPrice | askQty | |
|---|---|---|---|---|---|
| 0 | ETHBTC | 0.068918 | 1.7195 | 0.068919 | 0.0219 |
| 1 | LTCBTC | 0.002926 | 7.943 | 0.002927 | 4.368 |
| 2 | BNBBTC | 0.009438 | 4.493 | 0.009439 | 3.072 |
| 3 | NEOBTC | 0.000499 | 385.33 | 0.0005 | 793.74 |
| 4 | QTUMETH | 0.002231 | 304.3 | 0.002235 | 60.9 |
As per your comment, you need the pair of coins ending with USDT. Therefore what you need is to filter the dataframe out using a regex statement:
import requests
import json
import pandas as pd
data=requests.get('https://api.binance.com' + '/api/v1/ticker/allBookTickers')
data=json.loads(data.content)
dataframe = pd.DataFrame(data)
dataframe = dataframe[dataframe["symbol"].str.contains("USDT$")]
dataframe.to_excel("my_data.xlsx")
dataframe
which results in an output such as what follows:
| symbol | bidPrice | bidQty | askPrice | askQty | |
|---|---|---|---|---|---|
| 11 | BTCUSDT | 44260 | 0.11608 | 44260 | 1.56671 |
| 12 | ETHUSDT | 3116.59 | 5.0673 | 3116.6 | 12.3602 |
| 98 | BNBUSDT | 428.2 | 124.404 | 428.3 | 45.021 |
| 125 | BCCUSDT | 0 | 0 | 0 | 0 |
Note that I have shown just the first four rows of the dataframe.
Explanation
The regex USDT$ points to the strings which end (the dollar sign) with USDT.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
