如何将json响应分解为特定于名称值的多个部分?



嗨,我正在对myfxbook API进行API调用,作为回报,我正在接收一个数据过多的json响应,是否有可能将大响应分解为多个较小的部分,相对于名称值是什么?以下是大约30%的Json响应:

[{'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}]

你可以看到'name': 'EURUSD'有多个值,如短百分比和长百分比,短交易量和长交易量等。

我试图得到这些短期百分比和长期百分比的特定货币,而不是所有,

像这样:

EURUSD    32    68
GBPUSD    54    46
USDJPY    .......
:
:
:

我希望我的问题有意义

使用理解:

# 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 is shotred list:

[{'pair': 'EURUSD', 'short': 32, 'long': 68}, {'pair': 'GBPUSD', 'short': 54, 'long': 46}]

这是你想要的吗?

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"))

输出:

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")

输出:

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 

如果你只想要一些特定的货币,使用条件语句只打印那些需要的货币。

最新更新