【问题标题】:Print azure cosmos db container [duplicate]打印 azure cosmos db 容器 [重复]
【发布时间】:2020-02-09 05:11:16
【问题描述】:

我在 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

【问题讨论】:

  • 如果您在浏览器中搜索“Python 输出教程”,您会找到比我们在此处管理的更能解释这一点的参考资料。

标签: python json azure dictionary


【解决方案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)     

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-31
    • 2020-06-23
    • 1970-01-01
    • 2019-01-04
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多