【问题标题】:I need return JSON as beauty table style我需要将 JSON 作为美容表样式返回
【发布时间】:2021-12-13 00:21:06
【问题描述】:

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


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
    },
]

我需要一种方法将上述输出转换为美表或类似的:

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

请分享您使用此类工具的经验。

谢谢

【问题讨论】:

    标签: python json xml python-requests prettytable


    【解决方案1】:

    PrettyTableBeautifulTable 都有。您可以 pip 安装这些并阅读文档。如果你真的想做一些有用的东西,OpenpyXl 就是你要走的路。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-05
      • 2021-01-17
      • 2019-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-18
      相关资源
      最近更新 更多