Python 如何打印字典列表
我有以下清单:Python 如何打印字典列表,python,json,list,dictionary,pretty-print,Python,Json,List,Dictionary,Pretty Print,我有以下清单: 导入json 导入pprint 评分字典=[{'IMEI':'867', “时间戳”:“2020-07-02 13-29-24”, “RAG”:“G”, “概率”:0.12}, {'IMEI':'430', “时间戳”:“2020-07-02 13-29-24”, “RAG”:“G”, “概率”:0.12}, {'IMEI':'658', “时间戳”:“2020-07-02 13-29-24”, “RAG”:“G”, “概率”:0.12}, {'IMEI':'157', “时间戳
导入json
导入pprint
评分字典=[{'IMEI':'867',
“时间戳”:“2020-07-02 13-29-24”,
“RAG”:“G”,
“概率”:0.12},
{'IMEI':'430',
“时间戳”:“2020-07-02 13-29-24”,
“RAG”:“G”,
“概率”:0.12},
{'IMEI':'658',
“时间戳”:“2020-07-02 13-29-24”,
“RAG”:“G”,
“概率”:0.12},
{'IMEI':'157',
“时间戳”:“2020-07-02 13-29-24”,
“RAG”:“G”,
“概率”:0.12},
{'IMEI':'521',
“时间戳”:“2020-07-02 13-29-24”,
“RAG”:“G”,
“概率”:0.12}]
我想把它打印在一个单元格的标准输出中。
在网上看到类似问题的代码后
print(“每个计分资产id的字典列表:{0}”。格式(json.dumps(json.loads(scoring_dictionary),indent=4)))
但产出仍然很糟糕
“每个评分资产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','Probability':0.12}.]”一个字符串的纯文本,没有任何注释
除了字典列表之外,我还想打印一本字典,它看起来像:
{'failed_devices':['334','897','485','partially_successed_devices':['867','430','658','157','521','total_failures':705,'total_partially_successed':268,'total_successed':999,'timestamp':'2020-07-02 13-29-24','failures_threshold':0.1,'failured_占总数的百分比:0.7057057057}
我想以与上面字典列表相同的方式打印它
[更新]
这比pprint更有效。感谢所有的答案。谢谢你的关心
print(“描述完整批评分执行的sotred度量列表:{0}”。格式(json.dumps(scoring_dictionary,indent=2)))
提前感谢您的帮助。要获得更漂亮的印花,您可以查看:
#词典列表
评分字典=[{'IMEI':'358639059721867',
“时间戳”:“2020-07-02 13-29-24”,
“RAG”:“G”,
“概率”:0.12},
{'IMEI':'358639059721430',
“时间戳”:“2020-07-02 13-29-24”,
“RAG”:“G”,
“概率”:0.12},
{'IMEI':'358639059721658',
“时间戳”:“2020-07-02 13-29-24”,
“RAG”:“G”,
“概率”:0.12},
{'IMEI':'358639059721157',
“时间戳”:“2020-07-02 13-29-24”,
“RAG”:“G”,
“概率”:0.12},
{'IMEI':'358639059721521',
“时间戳”:“2020-07-02 13-29-24”,
“RAG”:“G”,
“概率”:0.12},
{'IMEI':'358639059721713',
“时间戳”:“2020-07-02 13-29-24”,
“RAG”:“G”,
“概率”:0.12}]
#字典
设备成功失败={'failed'u devices':['334','897','485','partially'u succeed'u devices':['867','430','658','157','521','total'u failures':705','total'u partially'u succeed':268','total'succeed':26','total':999','timestamp':'2020-07-02 13-29-24','failures's'threshold':0.1','failed'u out'u总数的百分比:0.7057057057057057}
#适用于这两种情况
打印(“描述完整批评分执行的sotred度量列表:{0}”。格式(json.dumps(scoring_dictionary,indent=2)))
打印(“描述完整批量评分执行的sotred度量列表:{0}”。格式(json.dumps(devices\u success\u failed,indent=2)))
@CoryKramer这是我看到的一篇文章,但我有完全相同的输出。我会再试一次,但我认为不会有什么变化,这不是json.dumps的输出,因为json必须使用双引号,而不是单引号。为print()显示的代码与显示的输出不匹配。您也没有插入stringTry所需的格式“{}”。在这种情况下,来自pprint导入打印的;打印(lst)
,其中lst是列表。[{'IMEI':'867',
不是有效的JSON。您的代码与输出不匹配。
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'}]