Skip to content

Instantly share code, notes, and snippets.

@ericdorsey
Last active May 22, 2016 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericdorsey/6734172a9d0632ae7d9d884a01dd565e to your computer and use it in GitHub Desktop.
Save ericdorsey/6734172a9d0632ae7d9d884a01dd565e to your computer and use it in GitHub Desktop.

iPython "magic" %cpaste; assign to a variable

In iPython:

~ $ ipython
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
Type "copyright", "credits" or "license" for more information.

IPython 2.3.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]:

Assign what you're about to paste to variabe currToParse, paste from clipboard, end the %cpaste "session" with -- and hit enter to finish assigning to the variable:

In [90]: %cpaste currToParse
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:
{ugly multiline code you paste from clipboard}
:--

Block assigned to 'currToParse'

Join into a single string:

In [91]: currToParse = "\n".join(currToParse)

No we can manipulate this monstrosity of ugly multiline HTML code etc. from inside the iPython interpreter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment