0

I have the following list:

import json
import pprint

scoring_dictionary=[{'IMEI': '867',
  'Timestamp': '2020-07-02 13-29-24',
  'RAG': 'G',
  'Probability': 0.12},
 {'IMEI': '430',
  'Timestamp': '2020-07-02 13-29-24',
  'RAG': 'G',
  'Probability': 0.12},
 {'IMEI': '658',
  'Timestamp': '2020-07-02 13-29-24',
  'RAG': 'G',
  'Probability': 0.12},
 {'IMEI': '157',
  'Timestamp': '2020-07-02 13-29-24',
  'RAG': 'G',
  'Probability': 0.12},
 {'IMEI': '521',
  'Timestamp': '2020-07-02 13-29-24',
  'RAG': 'G',
  'Probability': 0.12}]

And I want to pretty print this in the standard output of a cell. Having seen this code on an online similar question,

print("The list of dictionaries per scored asset id: {0}".format(json.dumps(json.loads(scoring_dictionary), indent=4)))

But the output still remains bad

"The list of dictionaries per scored asset id: [{'IMEI': '867', 'Timestamp': '2020-07-02 13-29-24', 'RAG': 'G', 'Probability': 0.12}, {'IMEI': '430', 'Timestamp': '2020-07-02 13-29-24', 'RAG': 'G', 'Probability': 0.12}...]" # a straight text of string without intends

Apart from the list of dictionaries I want also to pretty print a dictionary which seems like:

{'failed_devices': ['334', '897', '485'], 'partially_succeeded_devices': ['867', '430', '658', '157', '521'], 'total_failures': 705, 'total_partially_succeeded': 268, 'total_succeeded': 26, 'total': 999, 'timestamp': '2020-07-02 13-29-24', 'failures_threshold': 0.1, 'failed_percentage_out_of_total': 0.7057057057057057}

I want to pretty print it in the same exact way like the above list of dictionaries.

[UPDATE] This worked better than pprint. Thanks to all answers. Appreciate your concern.

print("The list of sotred metrics depicting the complete batch scoring execution: {0}".format(json.dumps(scoring_dictionary, indent=2)))

Thank you in advance for any help.

pppery
  • 3,731
  • 22
  • 33
  • 46
NikSp
  • 1,262
  • 2
  • 19
  • 42
  • 1
    https://stackoverflow.com/questions/12943819/how-to-prettyprint-a-json-file – Cory Kramer Jul 02 '20 at 19:30
  • @CoryKramer This was the one post I saw but I have the exact same output. I will retry it but I don't think something will change – NikSp Jul 02 '20 at 19:32
  • that's not the output of json.dumps, as JSON must use double quotes, not single quotes. the code you have shown for print() does not match the output you have shown. You also don't have "{}" which is necessary for format to insert the string – user120242 Jul 02 '20 at 19:33
  • Try [pprint](https://docs.python.org/3/library/pprint.html). In this case `from pprint import print; print(lst)`, where lst is the list. – DarrylG Jul 02 '20 at 19:36
  • `[{'IMEI': '867',` is not valid JSON. Your code does not match the output. – user120242 Jul 02 '20 at 19:40
  • @user120242 Should I use " " double code instead? Because Python does not give me an error on that – NikSp Jul 02 '20 at 19:42
  • @NikSp what I'm saying is that the string you have is coming from somewhere else some other code, not the json.dumps you have there. That code would not produce a string with single quotes in it. it's more likely you have a print("The list...") followed later by print(devices_succeed_failed) – user120242 Jul 02 '20 at 19:43
  • @user120242 please check my update :) – NikSp Jul 02 '20 at 19:52
  • @CoryKramer I rechecked the posr and you were right json.dumps works fine – NikSp Jul 02 '20 at 19:54

2 Answers2

2

For prettier prints you can check out pprint:

import pprint 
pprint.pprint(data)


pprint.pprint(data)

[{'IMEI': '867',
  'Probability': 0.12,
  'RAG': 'G',
  'Timestamp': '2020-07-02 13-29-24'},
 {'IMEI': '430',
  'Probability': 0.12,
  'RAG': 'G',
  'Timestamp': '2020-07-02 13-29-24'},
 {'IMEI': '658',
  'Probability': 0.12,
  'RAG': 'G',
  'Timestamp': '2020-07-02 13-29-24'},
 {'IMEI': '157',
  'Probability': 0.12,
  'RAG': 'G',
  'Timestamp': '2020-07-02 13-29-24'},
 {'IMEI': '521',
  'Probability': 0.12,
  'RAG': 'G',
  'Timestamp': '2020-07-02 13-29-24'}]
Remolten
  • 2,614
  • 2
  • 25
  • 29
0

Similar question

#list of dictionaries
scoring_dictionary=[{'IMEI': '358639059721867',
  'Timestamp': '2020-07-02 13-29-24',
  'RAG': 'G',
  'Probability': 0.12},
 {'IMEI': '358639059721430',
  'Timestamp': '2020-07-02 13-29-24',
  'RAG': 'G',
  'Probability': 0.12},
 {'IMEI': '358639059721658',
  'Timestamp': '2020-07-02 13-29-24',
  'RAG': 'G',
  'Probability': 0.12},
 {'IMEI': '358639059721157',
  'Timestamp': '2020-07-02 13-29-24',
  'RAG': 'G',
  'Probability': 0.12},
 {'IMEI': '358639059721521',
  'Timestamp': '2020-07-02 13-29-24',
  'RAG': 'G',
  'Probability': 0.12},
 {'IMEI': '358639059721713',
  'Timestamp': '2020-07-02 13-29-24',
  'RAG': 'G',
  'Probability': 0.12}]

#dictionary
devices_succeed_failed={'failed_devices': ['334', '897', '485'], 'partially_succeeded_devices': ['867', '430', '658', '157', '521'], 'total_failures': 705, 'total_partially_succeeded': 268, 'total_succeeded': 26, 'total': 999, 'timestamp': '2020-07-02 13-29-24', 'failures_threshold': 0.1, 'failed_percentage_out_of_total': 0.7057057057057057}
# WORKED FOR BOTH CASES
print("The list of sotred metrics depicting the complete batch scoring execution: {0}".format(json.dumps(scoring_dictionary, indent=2)))

print("The list of sotred metrics depicting the complete batch scoring execution: {0}".format(json.dumps(devices_succeed_failed, indent=2)))
NikSp
  • 1,262
  • 2
  • 19
  • 42