如何漂亮地打印字典列表[关闭]

问题描述 投票:0回答:2

我有以下清单:

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}]

我想在单元格的标准输出中漂亮地打印它。 在网上类似的问题上看到这段代码,

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

但输出仍然很差

"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

除了字典列表之外,我还想漂亮地打印一本字典,看起来像:

{'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}

我想以与上面的词典列表相同的方式漂亮地打印它。

[更新] 这比 pprint 效果更好。感谢所有的答案。感谢您的关心。

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

提前感谢您的帮助。

python json list dictionary pretty-print
2个回答
2
投票

要获得更漂亮的打印效果,您可以查看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'}]

0
投票

类似问题

#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)))
© www.soinside.com 2019 - 2024. All rights reserved.