2

The question's title tells it; I'm reading a book and I'd like to try the code on the fly using IPython but all the code is structured like this:

right = DataFrame({'key1': ['foo', 'foo', 'bar', 'bar'],
....: 'key2': ['one', 'one', 'one', 'two'],
....: 'rval': [4, 5, 6, 7]})

I'd like to copy it directly from the book inside the terminal but even using %paste I receive an Invalid Syntax error. I could use %cpaste but for longer inputs it is kind of frustrating.

Thanks for your help

7
  • 1
    It seems that %paste only detects and strips pure python's continuation prompt ... , but not ipython's ....: . So changing the prompts to ... would work. But that's probably not an option for you. Commented Oct 10, 2014 at 12:26
  • Exactly, because the code is from a book not from my prompt Commented Oct 10, 2014 at 12:31
  • IPython should be able to handle this. The docs say IPython session code can be pasted since version 1. Perhaps it's a regression that hasn't been noticed (bug). ipython.org/ipython-doc/2/interactive/… Commented Oct 10, 2014 at 13:20
  • Your problem is that you want to remove the dots ? Commented Oct 10, 2014 at 13:42
  • 1
    @jeanrjc: I don't think so - the problem is that ipython's paste magic claims to be able to handle ipython prompts, but apparently that is not the case. It's probably a good idea to file an issue at ipython's issue tracker: github.com/ipython/ipython/issues Commented Oct 10, 2014 at 13:47

1 Answer 1

1

So it should work, but you need to be sure that when pasting the ....: are well aligned. Meaning that you need to copy carefully.

The following should work for instance :

right = DataFrame({'key1': ['foo', 'foo', 'bar', 'bar'],
   ....: 'key2': ['one', 'one', 'one', 'two'],
   ....: 'rval': [4, 5, 6, 7]})

Or this one too (where we see the alignment with the semi column of In []: :

In [68]: a = [1,
   ....: 2,
   ....: 3]

My guess that if you cannot copy better than you did, is that the book did a bad formatting when pasting. If so, you still can open a basic text editor and find and replace ....: with nothing.

Instead of opening an issue, it should be more probably related to a feature of the %paste function which implement something that deals with bad indentation, but then it starts being messy, IMO.

Hope this helps.

Sign up to request clarification or add additional context in comments.

3 Comments

As you pointed out your examples work fine, the problem is that while copy pasting from the book it doesn't preserve the spaces before ....: and so the indentation is destroyed. I think though that IPython should be able to handle this
Maybe because it's a pdf. What book is it ?
I know, but then the editor should know that it's a bad idea to let the ...: if one can't copy past afterward.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.