Reputation: 1625
A typeo produced an unexpected result and just in case it has a use, I want to understand it.
!!C:/myPythonPath/python script/event.py
Single !
runs a command (DOS, LINUX, other commands you could normally execute from a command line) from within Python, in this case, from with a Jupyter Notebook cell of Python 2.7.
I mistakenly typed two exclamation points and surprisingly, the code still worked, but my output appeared inside of square brackets as in:
[ output of my program ]
What's more, the presentation looks like it put the output in a list. Here is an example:
['------------------------------',
'Unknown Event: Django Girls Bucaramanga, Colombia',
' Time : 08 April – 09 April 2017',
' Location: Bucaramanga, Colombia',
'------------------------------',
'Upcoming Event: Python ... ]
If this is truly a list, is there a way to capture it for later use in a program that is calling a script? I tried adding in an x = into the original line, but this did not work for me. Maybe I did not set up the line right. Is there a way to do this? This is just a point of curiosity.
Upvotes: 1
Views: 769
Reputation: 2738
All the details here -> https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-sx
copy/paste:
%sx¶
Shell execute - run shell command and capture output (!! is short-hand).
%sx command
IPython will run the given command using commands.getoutput(), and
return the result formatted as a list (split on ‘n’). Since the output is
_returned_, it will be stored in ipython’s regular output cache Out[N] and in
the ‘_N’ automatic variables.
I performed some tests (full code below so you can check yourself)
str_x="some text x"
str_y="some text y"
str_z="some text z"
%sx echo {str_x} ;echo -e {str_x}
_
x = %sx echo {str_x}; echo {str_x}
x
!echo {str_y}; echo {str_y}
y = !echo {str_y}; echo {str_y}
y
!!echo {str_z}; echo {str_z}
_
z = !!echo {str_z}; echo {str_z}
_
z
Individual outputs (python 3.7)
Some conclusions:
!!
is a shortcut for %sx
!!
cannot be assigned to variable, but the ouput is on _
!
and %sx
can be assigned to a variable, in any case ouput is on _
!
output is natural, while %sx
output is a list of the output (the output converted to a list)If you look at the first link, %sx
is a special case for %sc
.
%sx differs from %sc in that %sx automatically splits into a list, like ‘%sc -l’. The reason for this is to make it as easy as possible to process line-oriented shell output via further python commands. %sc is meant to provide much finer control, but requires more typing.
Note: Don't be fooled by the last _
. The command before it failed, so what it stores is the output of the previous one.
!!echo {str_z}; echo {str_z}
_
z = !!echo {str_z}; echo {str_z}
_ # This one is not the output of the preceding line
z
Upvotes: 0
Reputation: 1625
Comments provided by @user2357112 held the answer. Admittedly, you have to find it in a sea of other useful topics, and the actual syntax to solve the problem is not clearly stated in the write-ups.
The answer: Yes, !!
does convert output of !
into a list. But attempts to capture it from !!
, as the links below indicate is possible, do not seem to work.
Again from @user2357112, this syntax (which does not use the !!
) will achieve the desired effect (run the command and capture it in a list):
x = %sx echo "some text"
Print x
and you will get:
["some text"]
Links for more reading so they don't get buried in the comments:
Upvotes: 1