将数据通过管道传输到 IPython 并在处理后保留在会话中
Pipe data into IPython and remain in the session after processing it
我想将一些数据通过管道传输到 ipython
终端客户端,运行 一个命令,然后留在会话中。
就将参数传递给 IPython、运行 执行命令并保持会话活动而言,这有效:
[~]$ ipython -i -c "import sys; print(sys.argv[-1])" "my,testcsv\n1,2,3"
Python 3.7.3 (default, Mar 27 2019, 16:54:48)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.23.1 -- An enhanced Interactive Python. Type '?' for help.
my,test,csv\n1,2,3
In [1]:
但是添加管道既会在 Python 中引发异常,又会立即返回到 shell:
[~]$ echo "my,test,csv\n1,2,3" | ipython -i -c "import sys; print(sys.argv[-1])"
Python 3.7.3 (default, Mar 27 2019, 16:54:48)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.23.1 -- An enhanced Interactive Python. Type '?' for help.
import sys; print(sys.argv[-1])
In [1]: File "<ipython-input-1-f826f34ba2b7>", line 1
my,test,csv\n1,2,3
^
SyntaxError: unexpected character after line continuation character
In [2]: Do you really want to exit ([y]/n)?
[~]$ ^
并使用 xargs
正确处理输入,但它也会立即退出回到 shell:
[~]$ echo "my,test,csv\n1,2,3" | xargs -0 ipython -i -c "import sys; print(sys.argv[-1])"
Python 3.7.3 (default, Mar 27 2019, 16:54:48)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.23.1 -- An enhanced Interactive Python. Type '?' for help.
my,test,csv\n1,2,3
In [1]: Do you really want to exit ([y]/n)?
[~]$
有什么方法可以实现我在这里的目标吗?
我认为它在从标准输入读取 EOF 时正在退出。 true | ipython -i
也立即退出。就此而言,true | python -i
.
也是如此
相反,您可以使用 process substitution 将命令的标准输出作为文件获取。
tmp.py
:
import sys
fname = sys.argv[-1]
with open(fname) as f:
print(f.read())
$ ipython --no-banner -i tmp.py <(echo "foo")
foo
In [1]:
我想将一些数据通过管道传输到 ipython
终端客户端,运行 一个命令,然后留在会话中。
就将参数传递给 IPython、运行 执行命令并保持会话活动而言,这有效:
[~]$ ipython -i -c "import sys; print(sys.argv[-1])" "my,testcsv\n1,2,3"
Python 3.7.3 (default, Mar 27 2019, 16:54:48)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.23.1 -- An enhanced Interactive Python. Type '?' for help.
my,test,csv\n1,2,3
In [1]:
但是添加管道既会在 Python 中引发异常,又会立即返回到 shell:
[~]$ echo "my,test,csv\n1,2,3" | ipython -i -c "import sys; print(sys.argv[-1])"
Python 3.7.3 (default, Mar 27 2019, 16:54:48)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.23.1 -- An enhanced Interactive Python. Type '?' for help.
import sys; print(sys.argv[-1])
In [1]: File "<ipython-input-1-f826f34ba2b7>", line 1
my,test,csv\n1,2,3
^
SyntaxError: unexpected character after line continuation character
In [2]: Do you really want to exit ([y]/n)?
[~]$ ^
并使用 xargs
正确处理输入,但它也会立即退出回到 shell:
[~]$ echo "my,test,csv\n1,2,3" | xargs -0 ipython -i -c "import sys; print(sys.argv[-1])"
Python 3.7.3 (default, Mar 27 2019, 16:54:48)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.23.1 -- An enhanced Interactive Python. Type '?' for help.
my,test,csv\n1,2,3
In [1]: Do you really want to exit ([y]/n)?
[~]$
有什么方法可以实现我在这里的目标吗?
我认为它在从标准输入读取 EOF 时正在退出。 true | ipython -i
也立即退出。就此而言,true | python -i
.
相反,您可以使用 process substitution 将命令的标准输出作为文件获取。
tmp.py
:
import sys
fname = sys.argv[-1]
with open(fname) as f:
print(f.read())
$ ipython --no-banner -i tmp.py <(echo "foo")
foo
In [1]: