Pipe Ipython magic output to a variable?

后端 未结 3 825
面向向阳花
面向向阳花 2020-12-09 15:26

I want to run a bash script in my ipython Notebook and save the output as a string in a python variable for further manipulation. Basically I want to pipe the output of the

相关标签:
3条回答
  • 2020-12-09 16:00

    For completeness, if you would still like to use the %%bash cell magic, you can pass the --out and --err flags to redirect the outputs from the stdout and stderr to a variable of your choice.

    From the doucumentation:

    %%bash --out output --err error
    echo "hi, stdout"
    echo "hello, stderr" >&2
    

    will store the outputs in the variables output and error so that:

    print(error)
    print(output)
    

    will print to the python console:

    hello, stderr
    hi, stdout
    
    0 讨论(0)
  • 2020-12-09 16:09

    Notice the difference in the variable type between @MattDMo (SList) and @oLas (str) answers:

    In [1]: output = !whoami
    
    In [2]: type(output)
    Out[2]: IPython.utils.text.SList
    
    In [3]: %%bash --out output
       ...: whoami
    
    In [4]: type(output)
    Out[4]: str
    
    0 讨论(0)
  • 2020-12-09 16:14

    What about using this:

    myvar = !some_command --option1 --option2 foo bar
    

    instead of the %%bash magic? Using the ! symbol runs the following command as a shell command, and the results are all stored in myvar. For running multiple commands and collecting the output of all of them, just put together a quick shell script.

    0 讨论(0)
提交回复
热议问题