首页 > 解决方案 > PrettyPrinter - 在开始和结束括号的不同行上打印输出

问题描述

使用类似的东西:

pp = pprint.PrettyPrinter(indent=4, width=...).pprint

当前输出pp(my_list)

[   1,
    2,
    3]

期望的输出:

[
    1,
    2,
    3
]

如何做到这一点?

标签: pythonpretty-print

解决方案


使用json 模块

前任:

import json
my_list = [1,2,3]
print(json.dumps(my_list, indent=4))

输出:

[
    1, 
    2, 
    3
]

推荐阅读