管道 Ipython 魔术输出到变量? [英] Pipe Ipython magic output to a variable?

查看:26
本文介绍了管道 Ipython 魔术输出到变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 ipython Notebook 中运行一个 bash 脚本并将输出保存为 python 变量中的字符串以供进一步操作.基本上我想将 bash 魔法的输出通过管道传输到一个变量,例如这样的输出:

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 bash magic to a variable, For example the output of something like this:

%%bash
some_command [options] foo bar

推荐答案

使用这个怎么样:

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

而不是 %%bash 魔法?使用! 符号将以下命令作为shell 命令运行,结果全部存储在myvar 中.要运行多个命令并收集所有命令的输出,只需编写一个快速的 shell 脚本即可.

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.

这篇关于管道 Ipython 魔术输出到变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆