gpt4 book ai didi

Python PrettyPrint 输出到变量

转载 作者:太空狗 更新时间:2023-10-29 20:42:27 26 4
gpt4 key购买 nike

如何将 Python PrettyPrint 输出存储到某个变量。

除了 eyeD3 还有其他方法吗?

像这样-

string_output = pp.pprint(dict)

最佳答案

使用 pprint.pformat功能:

>>> my_dict = dict((i, i) for i in range(30))
>>> pp.pformat(my_dict)
'{0: 0,\n 1: 1,\n 2: 2,\n 3: 3,\n 4: 4,\n 5: 5,\n 6: 6,\n 7: 7,\n 8: 8,\n 9: 9,\n 10: 10,\n 11: 11,\n 12: 12,\n 13: 13,\n 14: 14,\n 15: 15,\n 16: 16,\n 17: 17,\n 18: 18,\n 19: 19,\n 20: 20,\n 21: 21,\n 22: 22,\n 23: 23,\n 24: 24,\n 25: 25,\n 26: 26,\n 27: 27,\n 28: 28,\n 29: 29}'
>>> print(pp.pformat(my_dict))
{0: 0,
1: 1,
2: 2,
3: 3,
4: 4,
5: 5,
6: 6,
7: 7,
8: 8,
9: 9,
10: 10,
11: 11,
12: 12,
13: 13,
14: 14,
15: 15,
16: 16,
17: 17,
18: 18,
19: 19,
20: 20,
21: 21,
22: 22,
23: 23,
24: 24,
25: 25,
26: 26,
27: 27,
28: 28,
29: 29}

关于Python PrettyPrint 输出到变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17463483/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com