使用python将json接收的数据制成表格

2024-09-27 21:31:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我可以解析以下数据。但我不能将其显示为列式表格。我如何绘制图表

 for item in items:
     print(item['symbol'])
[
{"symbol": "ZILUSDT", "positionAmt": "0", "entryPrice": "0.00000", "markPrice": "0.01728152", "unRealizedProfit": "0.00000000", "liquidationPrice": "0", "leverage": "20", "maxNotionalValue": "25000", "marginType": "cross", "isolatedMargin": "0.00000000", "isAutoAddMargin": "false", "positionSide": "SHORT"}, 
{"symbol": "FLMUSDT", "positionAmt": "0", "entryPrice": "0.0000", "markPrice": "0.00000000", "unRealizedProfit": "0.00000000", "liquidationPrice": "0", "leverage": "20", "maxNotionalValue": "25000", "marginType": "cross", "isolatedMargin": "0.00000000", "isAutoAddMargin": "false", "positionSide": "BOTH"}, 
{"symbol": "FLMUSDT", "positionAmt": "0", "entryPrice": "0.0000", "markPrice": "0.00000000", "unRealizedProfit": "0.00000000", "liquidationPrice": "0", "leverage": "20", "maxNotionalValue": "25000", "marginType": "cross", "isolatedMargin": "0.00000000", "isAutoAddMargin": "false", "positionSide": "LONG"}, 
{"symbol": "FLMUSDT", "positionAmt": "0", "entryPrice": "0.0000", "markPrice": "0.00000000", "unRealizedProfit": "0.00000000", "liquidationPrice": "0", "leverage": "20", "maxNotionalValue": "25000", "marginType": "cross", "isolatedMargin": "0.00000000", "isAutoAddMargin": "false", "positionSide": "SHORT"}
]



How can I show it in a table like the one below?

     headers = ["Symbol", "EntryPrice", "side", "position"]
     symbols = [x['symbol'] for x in data]
     table = columnar(symbols, headers=headers)
     print(table)

Tags: infalsesymbolcrossunrealizedprofitleverageentrypricepositionamt
2条回答

你有一份字典的清单。最简单的方法是将它们包装到pandas的数据框中,该数据框可即时处理该类型

import pandas as pd

tabulated = pd.DataFrame(items)
print(tabulated)

Pandas是一个很好的解决方案,但您也可以以表格格式显示数据:

for row in zip(*([key] + (value) for key, value in sorted(items.items()))):
    print(*row)

相关问题 更多 >

    热门问题