将ipython脚本的输出重定向到csv或文本文件,如sqlplus spool [英] redirect output of ipython script into a csv or text file like sqlplus spool

查看:174
本文介绍了将ipython脚本的输出重定向到csv或文本文件,如sqlplus spool的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将脚本的输出重定向到文件。
我不想做类似的事情

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

由于很多变量存储在我的i​​python环境中,我喜欢随身携带结束。

as a lot of the variable is being stored in my ipython environment and I like to carry it over.

我尝试按照此链接

IPython:将Python脚本的输出重定向到文件(如bash>)

然而,当我尝试使用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

https: //www.quora.com/How-do-I-export-the-output-of-the-IPython-command-to-a-text-file-or-a-CSV-file

使用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 command e.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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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