【问题标题】:IPython: Print string resultsIPython:打印字符串结果
【发布时间】:2020-04-14 17:49:47
【问题描述】:
我想让 IPython 自动打印所有 str 结果。
In [13]: 'hi\nhi' # I don't want this output
Out[13]: 'hi\nhi'
In [14]: print(_) # I want it like this, but without using print on every result.
hi
hi
【问题讨论】:
标签:
python
ipython
ipython-magic
【解决方案1】:
这是一个非常肮脏的黑客,但你可以猴子补丁IPython.lib.pretty.RepresentationPrinter:
In [9]: import IPython.lib.pretty
In [10]: class NewPrettier(IPython.lib.pretty.RepresentationPrinter):
...: def pretty(self, obj):
...: if isinstance(obj, (str,)):
...: self.text(obj)
...: else:
...: super().pretty(obj)
...:
In [11]: IPython.lib.pretty.RepresentationPrinter = NewPrettier
In [12]: "a"
Out[12]: a
编辑:删除 Out[n]:
In [24]: import IPython.core.displayhook
In [25]: IPython.core.displayhook.DisplayHook.write_output_prompt = lambda :None
In [26]: "X"
X