我需要returnJSON作为美女table风格

I need return JSON as beauty table style

我有这个代码并且运行良好


import requests 
import xml.etree.ElementTree as ET
import json

def get_stock(sku):
    params = {'ItemId': sku}
    base_url = 'http://10.0.0.25/api/GetSku'
    response = requests.get(base_url, params=params)
    json_parsed = json.loads(response.content)
     
    if response.status_code == 200:
      return [item for item in response.json() if item['Site'] == "12"]
    else:
      return None

def jprint(obj):
    text = json.dumps(obj, sort_keys=True, indent=4)
    print(text)
jprint(get_stock(101025HRLONDON))

输出为json:

[
   {
        "ItemId": "101025HRLONDON",
        "Site": "12",
        "Warehouse": "17",
        "availablePhysical": 1.0
    },
    {
        "ItemId": "101025HRLONDON",
        "Site": "12",
        "Warehouse": "33",
        "availablePhysical": 1.0
    },
    {
        "ItemId": "101025HRLONDON",
        "Site": "12",
        "Warehouse": "34",
        "availablePhysical": 1.0
    },
]

我需要一种方法将上面的输出转换为美丽 table 或类似这样的:

+----+------+----------------------+
| WH |aval. | ItemID               |
+----+------+----------------------+
| 17 |    1 | 101025HRLONDON       |
| 33 |    1 | 101025HRLONDON       |
| 34 |    1 | 101025HRLONDON       |
+----+------+----------------------+

请分享您使用此类东西的经验。

谢谢

两者都有PrettyTable and BeautifulTable。您可以 pip 安装这些并阅读文档。如果您真的想做些有用的东西,OpenpyXl 是您的不二之选。