首页 > 解决方案 > 读取 ipython 神奇的“精度”值

问题描述

ipython您可以使用魔术功能设置打印精度precision

In [1]: %precision 2                                                            
Out[1]: '%.2f'

In [2]: 5/7                                                                     
Out[2]: 0.71

您还可以通过 ipython 对象发出命令:

ipython = get_ipython()
ipython.run_line_magic("precision", "2")

但是你如何获得字符串'%.2f'

有没有类似的命令ipython.get_magic_value('precision')

标签: ipython

解决方案


找到了解决方案:

from IPython import get_ipython
ipython = get_ipython()
ipython.run_line_magic("precision", "3")
# you could also type %precision 3

form = ipython.display_formatter.formatters['text/plain']

form.float_format                                                                                           
#yields: '%.3f'

form.float_precision                                                                                        
#yields '3'

推荐阅读