如何将Python shell中的输出格式化为多行?
在pythonshell和BPython中,我试图让输出自然地分成多行,就像在节点解释器中一样 这里有一个例子来说明我的意思:如何将Python shell中的输出格式化为多行?,python,python-3.x,shell,bpython,Python,Python 3.x,Shell,Bpython,在pythonshell和BPython中,我试图让输出自然地分成多行,就像在节点解释器中一样 这里有一个例子来说明我的意思: # This is what is currently happening # (I'm truncating output, but the response is actually a lot longer than this): >>> response.dict {'status': '200', 'accept-ranges': 'byt
# This is what is currently happening
# (I'm truncating output, but the response is actually a lot longer than this):
>>> response.dict
{'status': '200', 'accept-ranges': 'bytes', 'cache-control': 'max-age=604800'}
# Here's what I'd like to happen:
>>> response.dict
{'status': '200',
'accept-ranges': 'bytes',
'cache-control': 'max-age=604800'}
这将使阅读变得容易得多。有人知道是否可以在BPython或普通shell中配置它吗?(或者说实话,任何一个口译员——只要我愿意,我都会转用。)
非常感谢
编辑:谢谢大家的回答!我以前遇到过漂亮的印刷品,并考虑过使用它。我还考虑过使用for循环在自己的行上打印所有内容。这些主意不错,但我希望我能让翻译自动完成 您可以使用:
结果:
{'accept-ranges': 'bytes',
'another item': 'another value',
'cache-control': 'max-age=604800',
'status': '200'}
您可以使用:
结果:
{'accept-ranges': 'bytes',
'another item': 'another value',
'cache-control': 'max-age=604800',
'status': '200'}
有几种方法可以做到这一点。您可以使用链接中显示的三重引号(“”)和“\n”。链接中还列出了其他方法作为stringl . 如果处理Json对象,可以使用以下代码,这应该会有所帮助
import json
with open('msgs.json', 'r') as json_file:
for row in json_file:
data = json.loads(row)
print json.dumps(data, sort_keys=True, indent=2, separators=(',', ': '))
有几种方法可以做到这一点。您可以使用三重引号(“”),如链接中所示,同时使用“\n”。其他方法也作为stringl列在链接中 . 如果处理Json对象,可以使用以下代码,这应该会有所帮助
import json
with open('msgs.json', 'r') as json_file:
for row in json_file:
data = json.loads(row)
print json.dumps(data, sort_keys=True, indent=2, separators=(',', ': '))
你能不能说得更具体一点,你想要什么触发换行?dict中的每个键/值对?有带分隔符的序列吗?@BradSolomon:我希望列表或dict中的每个条目都有自己的行。因此,键和值将共享一行,但在列表中,每个条目将位于其自己的行上。Node可以做到这一点,至少对中等大小和更长的DICT是这样。您试过IPython吗?默认情况下,它有很好的打印效果。是的,Ipython似乎是一个不错的选择。我早该想出一个独奏!你能不能说得更具体一点,你想要什么触发换行?dict中的每个键/值对?有带分隔符的序列吗?@BradSolomon:我希望列表或dict中的每个条目都有自己的行。因此,键和值将共享一行,但在列表中,每个条目将位于其自己的行上。Node可以做到这一点,至少对中等大小和更长的DICT是这样。您试过IPython吗?默认情况下,它有很好的打印效果。是的,Ipython似乎是一个不错的选择。我早该想出一个独奏!