[英]How do I take the price and turn it into a variable I can use
I am currently using and API and this is the output I am given.我目前正在使用 API,这是我得到的 output。
['{"open":{"price":367.88', '"time":1593696600532}', '"close":{"price":364.11', '"time":1593720000277}', '"high":370.47', '"low":363.64}']
I have saved it as a string and split it at the commas.我已将其保存为字符串并以逗号分隔。 I just want to know how I can just take the price out of this list.
我只是想知道如何才能将价格从这个列表中取出。 The price changes every time the function is run.
每次运行 function 时,价格都会发生变化。
You can use a JSON to get the value.您可以使用 JSON 来获取该值。 Please don't split your response with the ',' delimiter.
请不要用“,”分隔符分割您的回复。 Use string response instead.
请改用字符串响应。 Try using below code:
尝试使用以下代码:
import json
response = '{"open":{"price":367.88, "time":1593696600532}, "close":{"price":364.11, "time":1593720000277}, "high":370.47, "low":363.64}'
resp_dict = json.loads(response)
print(resp_dict["open"]["price"])
U should convert ur data to json by using json.loads()
.您应该使用
json.loads()
将您的数据转换为 json 。
import json
data = ['{"open":{"price":367.88', '"time":1593696600532}', '"close":{"price":364.11', '"time":1593720000277}', '"high":370.47', '"low":363.64}']
data = ','.join(data)
data_json = json.loads(data)
open_price = data_json['open']['price']
close_price = data_json['close']['price']
maybe this code will help you to find out.也许这段代码会帮助你找出答案。
>>> import json
>>> data = ['{"open":{"price":367.88', "time":1593696600532}', '"close":{"price":364.11', '"time":1593720000277}', "high":370.47, "low":363.64}']
>>> data = ','.join(data)
>>> print(data)
{"open":{"price":367.88,"time":1593696600532},"close":{"price":364.11,"time":1593720000277},"high":370.47,"low":363.64}
>>> jsondata = json.loads(data)
>>> print(jsondata)
{'open': {'price': 367.88, 'time': 1593696600532}, 'close': {'price': 364.11, 'time': 1593720000277}, 'high': 370.47, 'low': 363.64}
>>> print(jsondata['open']['price'])
367.88
>>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.