-1

I am using azure cosmos db in python and have trouble printing data the way i want.

My container:

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

Python code:

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

The output is:

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

The way output I want is:

id: test 
Lexicon: 
Eqt: UNKN
PN: abvcfgg
location
Region: China         
Country: China
  • If you search in your browser for "Python output tutorial", you'll find references that can explain this much better than we can manage here. – Prune Oct 11 '19 at 16:35

1 Answers1

1

Like this, but work only with dict nested one times.


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)     
Florian Bernard
  • 2,561
  • 1
  • 9
  • 22