本文介绍了python:忽略前导">>>"和“......”在互动模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多在线python示例显示了在每行之前具有正常前导>>>和...字符的交互式python会话。

Many online python examples show interactive python sessions with normal leading ">>>" and "..." characters before each line.

通常,没有办法复制此代码而不获取这些前缀。

Often, there's no way to copy this code without also getting these prefixes.

在这些情况下,如果我想在复制后将此代码重新粘贴到我自己的python解释器中,我必须这样做一些工作首先剥离这些前缀。

In these cases, if I want to re-paste this code into my own python interpreter after copying, I have to do some work to first strip off those prefixes.

有没有人知道如何让python或iPython(或任何其他python解释器)自动忽略前导>>> 和......粘贴在行上的字符?

Does anyone know of a way to get python or iPython (or any other python interpreter) to automatically ignore leading ">>>" and "..." characters on lines that are pasted in?

示例:

>>> if True:
...     print("x")
...


推荐答案

您只需关闭 autoindent 即可包含>>> ...

You just need to either switch off autoindent to include >>> and ... in a multiline paste:

In [14]: %autoindent
Automatic indentation is: OFF
In [15]: >>> for i in range(10):
   ....: ...     pass
   ....:

In [16]: >>> for i in range(10):
   ...: ...     pass
   ...: ...
In [17]: >>> for i in range(10):
   ...: ...     pass
   ...: ...

In [18]: %autoindent
Automatic indentation is: ON

In [19]: >>> for i in range(10):
   ....:     ...     pass
   ....:
  File "<ipython-input-17-5a70fbf9a5a4>", line 2
    ...     pass
    ^
SyntaxError: invalid syntax

或者不要复制>>> ,它会正常工作:

Or don't copy the >>> and it will work fine:

In [20]: %autoindent
Automatic indentation is: OFF

In [20]:  for i in range(10):
   ....: ...     pass
   ....:

这篇关于python:忽略前导&quot;&gt;&gt;&gt;&quot;和“......”在互动模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:15