【问题标题】:Tabulating data received with json with python用python对json接收到的数据进行制表
【发布时间】:2020-10-30 16:16:48
【问题描述】:

我可以解析以下数据。但我无法将其显示为柱状表格。我如何绘制这个图表?

 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)

【问题讨论】:

  • “图表”是什么意思?您的意思是要创建实际图像(即 JPEG 或 GIF)?
  • pypi.org/project/Columnar 没有我想在这里创建的人

标签: python json python-3.x python-2.7


【解决方案1】:

您有一个字典列表。最简单的方法是将它们包装到 pandas 的 DataFrame 中,它可以开箱即用地处理该类型。

import pandas as pd

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

【讨论】:

  • 对于项目中的 sym:tabulated = pd.DataFrame(sym['symbol'], sym['entryPrice']) print(tabulated)
【解决方案2】:

Pandas 是一个很好的解决方案,但您也可以原生地以表格格式呈现数据:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    • 1970-01-01
    • 2022-11-29
    • 1970-01-01
    • 2013-10-28
    • 2019-05-05
    相关资源
    最近更新 更多