2

I've always found this very annoying that whenever IDLE code is posted online it contains the ">>>" and "..." characters and is essentially impossible to copy/paste easily without prior clean up.

Is there easy way to get around that from either the code poster and, more importantly for me, the code user's perspective?

The question marked as a possible duplicate asks how to change the prompt in the console. What I'm asking rather is, when I'm copy/pasting code into my console such as the code below:

>>> import os
>>> os.getcwd()

How can I do so without running into the problem of my console saying

  File "<input>", line 1
    >>> import os
     ^
SyntaxError: invalid syntax
Jad S
  • 2,705
  • 6
  • 29
  • 49
  • 1
    Possible duplicate of [Change Python interactive prompt ">>>"](http://stackoverflow.com/questions/33683744/change-python-interactive-prompt) – Jacob Krall Feb 09 '17 at 17:00
  • I always appreciate the prompt myself, because it lets me know what was input and what was output. – Mark Ransom Feb 09 '17 at 17:02
  • I usually only use the interactive console when I want to show specifically "when I input this, I get that". The prompts are helpful there for visually separating input from output. If there's significant setup code, like functions, I'll compose that in a file and F5 it over to the interactive console. – glibdud Feb 09 '17 at 17:03
  • You could write a quick function to strip the prompt using the answers to http://stackoverflow.com/questions/579687/how-do-i-copy-a-string-to-the-clipboard-on-windows-using-python. I'm partial to my own answer to that question of course, but there are many to choose from. – Mark Ransom Feb 09 '17 at 17:13
  • If you're using a Windows *Powershell* or *Cygwin* terminal, you can just select the text using block-mode select, holding down `ALT`, while selecting the rectangular block and thus avoiding the REPL prompts. This should also work in *nix, but haven't tried it lately. – not2qubit Dec 30 '21 at 01:54

3 Answers3

4

I wrote a quick CodePen to do this – it very naively replaces all initial ">>> " or "... " sequences with nothing. Alternatively, you could use the following Python code:

import re
def remove_prompts(code):
    print(re.sub("^(>{3}|\.{3})( |$)", "", code, flags=re.M))

For example:

>>> remove_prompts("""
... Here's some code:
... >>> def foo():
... ...     return "bar"
... ...""")

def foo():
    return "bar"

>>> 
ash
  • 5,139
  • 2
  • 27
  • 39
2

You can use sys.ps1 and sys.ps2 to override the primary and secondary prompt of the interpreter.

>>> import sys
>>> sys.ps2 = '' # removing the secondary prompt '... '
>>> sys.ps1 = '' # removing the primary prompt '>>> '
1 + 3
4
(1 +
3
) 
4

Otherwise, you can use a text editor to remove the leading >>> and ... prompts.

(vim: :%s/^\(>>>\|\.\.\.\) //)

Jacob Krall
  • 28,341
  • 6
  • 66
  • 76
  • 1
    This will be very useful when I'm copy/pasting my own console code. However, how about when copy pasting someone else's console code, that doesn't have the `ps2` and `ps1` deactivated (aside from manually cleaning the code myself in a text editor)? – Jad S Feb 09 '17 at 17:11
1

Jacob's answer is a correct when it comes to posting console code.

However, for pasting console code into your own console it turns out that IPython which ships with the Anaconda distribution does not get tripped up over the '>>>' and '...' from IDLE console code:

In[7]: >>> import os
>>> os.getcwd()
Out[7]: 'C:\\Projects\\xlsparser'
Jad S
  • 2,705
  • 6
  • 29
  • 49