问题描述
我尝试将脚本的输出重定向到文件。
我不想做类似的事情
I try to redirect the output of my script to a file.I don't want to do something like
python myscript.py > xy.out
由于很多变量存储在我的ipython环境中,我喜欢随身携带结束。
as a lot of the variable is being stored in my ipython environment and I like to carry it over.
我尝试按照此链接
然而,当我尝试使用redirect_output执行
however, when I try to do
with redirect_output("my_output.txt"):
%run my_script.py
它给出错误
---> 10 self.sys_stdout = sys.stdout
NameError: global name 'sys' is not defined
有一个类似的解决方案将ipython shell的输出复制到文件但是它说我的单元格未定义
There is a similar solution to copy the output of a ipython shell to a file but It says my cell its not defined
使用ipython有更简单的方法或内置功能吗?
例如
Is there a easier way or build in feature with ipython that does that ??e.g.
在oracle sqlplus中有一个假脱机命令
例如
In oracle sqlplus there is a spool commande.g.
spool /tmp/abc
select * from table_x;
spool off
现在sql语句输出位于/ tmp / abc
now the sql statement output is in /tmp/abc
这是ipython的等价物吗?
Is such a equivalent for ipython ??
推荐答案
问候。我最终使用这个解决方案:
Greeting. I end up using this solution:
%%capture var2
%run -I script2
import sys
orig_stdout = sys.stdout
f = file('out5.txt', 'w')
sys.stdout = f
print var2
stdout = orig_stdout
f.close()
它不是很方便,但它有效!
It is not very handy but it works!
这篇关于将ipython脚本的输出重定向到csv或文本文件,如sqlplus spool的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!