如何漂亮地打印字典列表

时间:2020-07-02 19:28:40

标签: python json list dictionary pretty-print

我有以下列表:

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)))

在此先感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

对于更漂亮的打印,您可以签出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'}]

答案 1 :(得分:0)

Similar question

const VirtualizedTable = withStyles(styles)(MuiVirtualizedTable);

export default function DataPreview(props) {


const rows =  [{ One: 'one', Two: 'Two',Three: 'Three',Four:'Four', Five:'Five', Six:'Six'}]

return (
    <Paper style={{ height: 400, width:700, overflowX: 'auto'}}>
        <VirtualizedTable

         width={Object.keys(rows[0]).length*150}
        
            rowCount={rows.length}
            rowGetter={({ index }) => rows[index]}
            
            columns={Object.keys(rows[0]).map(head=>{return(
                {
                    minWidth:150,
                    label:head,
                    dataKey:head
                }
            )})}
        />
    </Paper>
 );
}
#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}