打印Azure Cosmos DB容器

时间:2019-10-11 16:27:25

标签: python json azure dictionary

我在python中使用azure cosmos db,无法按我想要的方式打印数据。

我的容器:

 {
        "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

1 个答案:

答案 0 :(得分:1)

像这样,但是只能与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)