[英]how to break down json response into multiple parts specific to the name value?
嗨,我正在對 myfxbook api 進行 api 調用,作為回報,我收到了 json 響應,其中包含太多數據部分,是否可以分解對多個較小部分的大響應? 這是 Json 響應的大約 30%:
[{'name': 'EURUSD', 'shortPercentage': 32, 'longPercentage': 68, 'shortVolume': 22685.66, 'longVolume': 47762.79, 'longPositions': 89968, 'shortPositions': 56612, 'totalPositions': 146580, 'avgShortPrice': 1.1678, 'avgLongPrice': 1.199}, {'name': 'GBPUSD', 'shortPercentage': 54, 'longPercentage': 46, 'shortVolume': 6462.95, 'longVolume': 5587.2, 'longPositions': 17783, 'shortPositions': 22240, 'totalPositions': 40023, 'avgShortPrice': 1.3592, 'avgLongPrice': 1.3929}, {'name': 'USDJPY', 'shortPercentage': 89, 'longPercentage': 11, 'shortVolume': 32447.66, 'longVolume': 4081.71, 'longPositions': 10941, 'shortPositions': 58722, 'totalPositions': 69663, 'avgShortPrice': 106.7526, 'avgLongPrice': 108.6437}, {'name': 'GBPJPY', 'shortPercentage': 79, 'longPercentage': 21, 'shortVolume': 2725.22, 'longVolume': 711.99, 'longPositions': 3210, 'shortPositions': 9743, 'totalPositions': 12953, 'avgShortPrice': 146.1479, 'avgLongPrice': 149.2243}, {'name': 'USDCAD', 'shortPercentage': 47, 'longPercentage': 53, 'shortVolume': 6235.58, 'longVolume': 6930.97, 'longPositions': 16121, 'shortPositions': 13276, 'totalPositions': 29397, 'avgShortPrice': 1.2658, 'avgLongPrice': 1.3025}, {'name': 'EURAUD', 'shortPercentage': 32, 'longPercentage': 68, 'shortVolume': 562.13, 'longVolume': 1170.82, 'longPositions': 4444, 'shortPositions': 3004, 'totalPositions': 7448, 'avgShortPrice': 1.5434, 'avgLongPrice': 1.5816}, {'name': 'EURJPY', 'shortPercentage': 74, 'longPercentage': 26, 'shortVolume': 10500.41, 'longVolume': 3672.26, 'longPositions': 8166, 'shortPositions': 25254, 'totalPositions': 33420, 'avgShortPrice': 126.1881, 'avgLongPrice': 128.6925}, {'name': 'AUDCAD', 'shortPercentage': 48, 'longPercentage': 52, 'shortVolume': 1559.83, 'longVolume': 1699.36, 'longPositions': 5943, 'shortPositions': 6278, 'totalPositions': 12221, 'avgShortPrice': 0.9561, 'avgLongPrice': 0.979}, {'name': 'AUDJPY', 'shortPercentage': 83, 'longPercentage': 17, 'shortVolume': 1614.66, 'longVolume': 322.98, 'longPositions': 1905, 'shortPositions': 6048, 'totalPositions': 7953, 'avgShortPrice': 78.9751, 'avgLongPrice': 83.5306}, {'name': 'AUDNZD', 'shortPercentage': 63, 'longPercentage': 37, 'shortVolume': 673.48, 'longVolume': 403.95, 'longPositions': 2123, 'shortPositions': 3785, 'totalPositions': 5908, 'avgShortPrice': 1.0644, 'avgLongPrice': 1.0779}]
正如您所看到的“名稱”:“EURUSD”有多個值,例如 shortpercentage 和 longpercerntage、shortvolume 和 long volume 等
我試圖為特定貨幣而不是全部獲得這些短百分比和長百分比,
像這樣:
EURUSD 32 68
GBPUSD 54 46
USDJPY .......
:
:
:
我希望我的問題有意義我盡力解釋
這是你想要的嗎?
from tabulate import tabulate
data = [{'name': 'EURUSD', 'shortPercentage': 32, 'longPercentage': 68, 'shortVolume': 22685.66, 'longVolume': 47762.79, 'longPositions': 89968, 'shortPositions': 56612, 'totalPositions': 146580, 'avgShortPrice': 1.1678, 'avgLongPrice': 1.199}, {'name': 'GBPUSD', 'shortPercentage': 54, 'longPercentage': 46, 'shortVolume': 6462.95, 'longVolume': 5587.2, 'longPositions': 17783, 'shortPositions': 22240, 'totalPositions': 40023, 'avgShortPrice': 1.3592, 'avgLongPrice': 1.3929}, {'name': 'USDJPY', 'shortPercentage': 89, 'longPercentage': 11, 'shortVolume': 32447.66, 'longVolume': 4081.71, 'longPositions': 10941, 'shortPositions': 58722, 'totalPositions': 69663, 'avgShortPrice': 106.7526, 'avgLongPrice': 108.6437}, {'name': 'GBPJPY', 'shortPercentage': 79, 'longPercentage': 21, 'shortVolume': 2725.22, 'longVolume': 711.99, 'longPositions': 3210, 'shortPositions': 9743, 'totalPositions': 12953, 'avgShortPrice': 146.1479, 'avgLongPrice': 149.2243}, {'name': 'USDCAD', 'shortPercentage': 47, 'longPercentage': 53, 'shortVolume': 6235.58, 'longVolume': 6930.97, 'longPositions': 16121, 'shortPositions': 13276, 'totalPositions': 29397, 'avgShortPrice': 1.2658, 'avgLongPrice': 1.3025}, {'name': 'EURAUD', 'shortPercentage': 32, 'longPercentage': 68, 'shortVolume': 562.13, 'longVolume': 1170.82, 'longPositions': 4444, 'shortPositions': 3004, 'totalPositions': 7448, 'avgShortPrice': 1.5434, 'avgLongPrice': 1.5816}, {'name': 'EURJPY', 'shortPercentage': 74, 'longPercentage': 26, 'shortVolume': 10500.41, 'longVolume': 3672.26, 'longPositions': 8166, 'shortPositions': 25254, 'totalPositions': 33420, 'avgShortPrice': 126.1881, 'avgLongPrice': 128.6925}, {'name': 'AUDCAD', 'shortPercentage': 48, 'longPercentage': 52, 'shortVolume': 1559.83, 'longVolume': 1699.36, 'longPositions': 5943, 'shortPositions': 6278, 'totalPositions': 12221, 'avgShortPrice': 0.9561, 'avgLongPrice': 0.979}, {'name': 'AUDJPY', 'shortPercentage': 83, 'longPercentage': 17, 'shortVolume': 1614.66, 'longVolume': 322.98, 'longPositions': 1905, 'shortPositions': 6048, 'totalPositions': 7953, 'avgShortPrice': 78.9751, 'avgLongPrice': 83.5306}, {'name': 'AUDNZD', 'shortPercentage': 63, 'longPercentage': 37, 'shortVolume': 673.48, 'longVolume': 403.95, 'longPositions': 2123, 'shortPositions': 3785, 'totalPositions': 5908, 'avgShortPrice': 1.0644, 'avgLongPrice': 1.0779}]
parsed = [[d["name"], d["shortPercentage"], d["longPercentage"]] for d in data]
print(tabulate(parsed, headers=["Name", "Short %", "Long %"], tablefmt="sql"))
Output:
Name Short % Long %
------ --------- --------
EURUSD 32 68
GBPUSD 54 46
USDJPY 89 11
GBPJPY 79 21
USDCAD 47 53
EURAUD 32 68
EURJPY 74 26
AUDCAD 48 52
AUDJPY 83 17
AUDNZD 63 37
根據我的假設,我希望這是您正在尋找的。
結果:
l=[{'name': 'EURUSD', 'shortPercentage': 32, 'longPercentage': 68, 'shortVolume': 22685.66, 'longVolume': 47762.79, 'longPositions': 89968, 'shortPositions': 56612, 'totalPositions': 146580, 'avgShortPrice': 1.1678, 'avgLongPrice': 1.199}, {'name': 'GBPUSD', 'shortPercentage': 54, 'longPercentage': 46, 'shortVolume': 6462.95, 'longVolume': 5587.2, 'longPositions': 17783, 'shortPositions': 22240, 'totalPositions': 40023, 'avgShortPrice': 1.3592, 'avgLongPrice': 1.3929}, {'name': 'USDJPY', 'shortPercentage': 89, 'longPercentage': 11, 'shortVolume': 32447.66, 'longVolume': 4081.71, 'longPositions': 10941, 'shortPositions': 58722, 'totalPositions': 69663, 'avgShortPrice': 106.7526, 'avgLongPrice': 108.6437}, {'name': 'GBPJPY', 'shortPercentage': 79, 'longPercentage': 21, 'shortVolume': 2725.22, 'longVolume': 711.99, 'longPositions': 3210, 'shortPositions': 9743, 'totalPositions': 12953, 'avgShortPrice': 146.1479, 'avgLongPrice': 149.2243}, {'name': 'USDCAD', 'shortPercentage': 47, 'longPercentage': 53, 'shortVolume': 6235.58, 'longVolume': 6930.97, 'longPositions': 16121, 'shortPositions': 13276, 'totalPositions': 29397, 'avgShortPrice': 1.2658, 'avgLongPrice': 1.3025}, {'name': 'EURAUD', 'shortPercentage': 32, 'longPercentage': 68, 'shortVolume': 562.13, 'longVolume': 1170.82, 'longPositions': 4444, 'shortPositions': 3004, 'totalPositions': 7448, 'avgShortPrice': 1.5434, 'avgLongPrice': 1.5816}, {'name': 'EURJPY', 'shortPercentage': 74, 'longPercentage': 26, 'shortVolume': 10500.41, 'longVolume': 3672.26, 'longPositions': 8166, 'shortPositions': 25254, 'totalPositions': 33420, 'avgShortPrice': 126.1881, 'avgLongPrice': 128.6925}, {'name': 'AUDCAD', 'shortPercentage': 48, 'longPercentage': 52, 'shortVolume': 1559.83, 'longVolume': 1699.36, 'longPositions': 5943, 'shortPositions': 6278, 'totalPositions': 12221, 'avgShortPrice': 0.9561, 'avgLongPrice': 0.979}, {'name': 'AUDJPY', 'shortPercentage': 83, 'longPercentage': 17, 'shortVolume': 1614.66, 'longVolume': 322.98, 'longPositions': 1905, 'shortPositions': 6048, 'totalPositions': 7953, 'avgShortPrice': 78.9751, 'avgLongPrice': 83.5306}, {'name': 'AUDNZD', 'shortPercentage': 63, 'longPercentage': 37, 'shortVolume': 673.48, 'longVolume': 403.95, 'longPositions': 2123, 'shortPositions': 3785, 'totalPositions': 5908, 'avgShortPrice': 1.0644, 'avgLongPrice': 1.0779}]
for i in range(len(l)):
print(l[i]["name"],end=" ")
print(l[i]["shortPercentage"],end=" ")
print(l[i]["longPercentage"],end=" ")
print("\n")
Output:
EURUSD 32 68
GBPUSD 54 46
USDJPY 89 11
GBPJPY 79 21
USDCAD 47 53
EURAUD 32 68
EURJPY 74 26
AUDCAD 48 52
AUDJPY 83 17
AUDNZD 63 37
如果您只需要某些特定貨幣,請使用條件語句僅打印需要的貨幣。
使用理解:
# data = this is an input big data
interested = ['EURUSD', 'GBPUSD']
result = [{'pair' : pair['name'],
'short' : pair['shortPercentage'],
'long' : pair['longPercentage']}
for pair in data if pair['name'] in interested]
print(result)
Output拍攝名單:
[{'pair': 'EURUSD', 'short': 32, 'long': 68}, {'pair': 'GBPUSD', 'short': 54, 'long': 46}]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.