在PHP中,我可以这样做:
echo '<pre>'
print_r($array);
echo '</pre>'
Run Code Online (Sandbox Code Playgroud)
在Python中,我目前只是这样做:
print the_list
Run Code Online (Sandbox Code Playgroud)
但是,这将导致大量数据.有没有办法将它很好地打印到可读的树中?(有缩进)?
Joh*_*ooy 138
from pprint import pprint
pprint(the_list)
Run Code Online (Sandbox Code Playgroud)
shx*_*fee 28
调试无需导入即可快速入侵,pprint
可以加入列表'\n'
.
>>> lst = ['foo', 'bar', 'spam', 'egg']
>>> print '\n'.join(lst)
foo
bar
spam
egg
Run Code Online (Sandbox Code Playgroud)
Ale*_*lli 19
你的意思是......
>>> print L
['this', 'is', 'a', ['and', 'a', 'sublist', 'too'], 'list', 'including', 'many', 'words', 'in', 'it']
>>> import pprint
>>> pprint.pprint(L)
['this',
'is',
'a',
['and', 'a', 'sublist', 'too'],
'list',
'including',
'many',
'words',
'in',
'it']
>>>
Run Code Online (Sandbox Code Playgroud)
...?从您的粗略描述,标准库模块pprint是第一个想到的东西; 但是,如果您可以描述示例输入和输出(以便不必学习PHP以帮助您;-),我们可能会提供更具体的帮助!
Mar*_*coP 17
只需通过"解压缩"print函数参数中的列表并使用换行符(\n)作为分隔符.
print(*lst,sep ='\n')
lst = ['foo', 'bar', 'spam', 'egg']
print(*lst, sep='\n')
foo
bar
spam
egg
Run Code Online (Sandbox Code Playgroud)
import json
some_list = ['one', 'two', 'three', 'four']
print(json.dumps(some_list, indent=4))
Run Code Online (Sandbox Code Playgroud)
输出:
[
"one",
"two",
"three",
"four"
]
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
123734 次 |
最近记录: |