打印azure cosmos数据库

2024-06-28 15:09:00 发布

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

我在python中使用azurecosmosdb,无法按我想要的方式打印数据

我的容器:

 {
        "id": "first",
        "lexicon": {
            "Eqt": "UNKN",
             "PN": "abvcfgg",
        },
        "location": {
            "Region": "China",
            "Country": "China"
         }
}

Python代码:

for item in list(results):
    print(results)

输出为:

{'id': 'test', 'lexicon': {'Eqt': 'UNKN', 'PN': 'abvcfgg'}, "location": { "Region": "China",         
"Country": "China"}

我想要的输出方式是:

id: test 
Lexicon: 
Eqt: UNKN
PN: abvcfgg
location
Region: China         
Country: China

Tags: 数据testid方式locationcountryresultsregion
1条回答
网友
1楼 · 发布于 2024-06-28 15:09:00

像这样,但只对dict嵌套一次


test = [{
        "id": "first",
        "lexicon": {
            "Eqt": "UNKN",
             "PN": "abvcfgg",
        },
        "location": {
            "Region": "China",
            "Country": "China"
         }
}]

def pretyprint(elements):
    for key, values in elements.items():
        if isinstance(values, dict):
            print(f"{key} :")
            for k, v in values.items():
                print(f"    {k} : {v}")
        else:
             print(f"{key} : {values}")


for row in test:
    pretyprint(row)     

相关问题 更多 >