忽略前导>>>的解释器字符和省略号

时间:2010-01-14 17:19:23

标签: python ide interpreter

我正在学习一些教程中的一些例子,其中有很多领先的>>>文本中的字符和省略号。这使得很难切割和粘贴到IPython解释器中,因为它不喜欢这些字符串。

我是否可以使用其他适当忽略和解释这些主要条款的翻译?

例如,我无法将以下内容直接粘贴到解释器中:

>>> d = dict(x.__array_interface__)
>>> d['shape'] = (3, 2, 5)
>>> d['strides'] = (20, 20, 4)

>>> class Arr:
...     __array_interface__ = d
...     base = x

2 个答案:

答案 0 :(得分:4)

IPython可以这样做(查看%paste magic命令)

答案 1 :(得分:0)

在任何情况下,都是通过python代码清理这些东西的方法:

import re
matcher= re.compile("(?m)^[.>]{3} ")
def cleanup(text):
    return matcher.sub('', text)

使用示例:

>>> print (cleanup(""">>> d = dict(x.__array_interface__)
>>> d['shape'] = (3, 2, 5)
>>> d['strides'] = (20, 20, 4)

>>> class Arr:
...     __array_interface__ = d
...     base = x"""))