简体   繁体   English

Python - 重新格式化来自 Binance 的 JSON API 响应

[英]Python - Reformat a JSON API response from Binance

So, according to Python-Binance's documentation, the thing I am getting back is a dictionary JSON response.因此,根据 Python-Binance 的文档,我得到的是字典 JSON 响应。 When I get the response it looks like this:当我收到回复时,它看起来像这样:

{'symbol': 'BTCUSDT', 'price': '37256.90000000'}

What I want to do is take that and reformat it so it looks like this:我想要做的是把它重新格式化,使它看起来像这样:

'$BTC @ 37256.900'

Basically removing the braces and all the other garbage that isn't needed.基本上去除大括号和所有其他不需要的垃圾。

The code I was using from the Binance API with Python-Binance and the way I was printing it:我使用的代码来自 Binance API 和Python-Binance以及我打印它的方式:

price = client.get_symbol_ticker(symbol="BTCUSDT")

from crypto_tracker import price
print(price)

Is this possible at all?这可能吗?

Python has a great library called json to help you take a json string and create a python dict from it. Python 有一个很棒的库,叫做json可以帮助您获取 json 字符串并从 itBDDZA 创建 Z23EEdict4347BDD755BFC6B7EE93。

From your example I have created the following code:从您的示例中,我创建了以下代码:

import json

## Given example string
json_string = '{"symbol": "BTCUSDT", "price": "37256.90000000"}'

## Create a python dict from json string using json library json.loads
json_dict = json.loads(json_string)

## What the new python dict looks like:
## json_dict = {'symbol': 'BTCUSDT', 'price': '37256.90000000'}

## Print your expected output
print(f'$BTC @ {json_dict["price"]}')

Print Output:打印 Output:

$BTC @ 37256.90000000

Using the python "json" library will automatically convert the json string to a python dict which you can then use any way you like (ie automatically removing all the extra "braces and... other garbage that isn't needed.").使用 python “json” 库将自动将 json 字符串转换为 python 字典,然后您可以使用任何您喜欢的方式(即自动删除所有额外的“大括号和......其他不需要的垃圾”)。

Let me know if this helped and if you have any questions!让我知道这是否有帮助,如果您有任何问题!

Edit:编辑:

Per more back and forth in comments I took a look at the Binance API.在评论中我来回查看了 Binance API。 The API endpoint your using is:您使用的 API 端点是:

GET /api/v3/ticker/price

To get information from this you would need to make an HTTP request to:要从中获取信息,您需要向以下位置发出 HTTP 请求:

https://api.binance.com/api/v3/ticker/price

Here is example working code:这是示例工作代码:

import requests
import json

## Send a GET request to binance api
## Since this is a get request no apikey / secret is needed
response = requests.get("https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT")

## Print the text from the get request
## Output: {"symbol":"BTCUSDT","price":"37211.17000000"}
print(response.text)

## Response.text is a string so we can use json.loads on it
ticket = json.loads(response.text)

## Now that ticket is a python dict of the json string
## response from binance we can print what you want
print(f'$BTC @ {ticket["price"]}')

## Output: $BTC @ 37221.87000000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM