使用 “!” 将 shell 命令的输出赋值给 .ipynb 文件中的变量。

huangapple go评论143阅读模式
英文:

Using ! to assign output of shell command to variable in .ipynb

问题

> ipython
Python 3.11.1 (main, Jan 24 2023, 17:02:06) [Clang 14.0.0 (clang-1400.0.29.202)]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.8.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: l = ! ls

In [2]: l[:3]
Out[2]: ['___x.ipynb', 'data', 'e.txt']

In [3]: l = (! ls)[:3]
  Cell In[3], line 1
    l = (! ls)[:3]
         ^
SyntaxError: invalid syntax
英文:

Is there a way to get this on one line:

> ipython
Python 3.11.1 (main, Jan 24 2023, 17:02:06) [Clang 14.0.0 (clang-1400.0.29.202)]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.8.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: l = ! ls

In [2]: l[:3]
Out[2]: ['___x.ipynb', 'data', 'e.txt']

In [3]: l = (! ls)[:3]
  Cell In[3], line 1
    l = (! ls)[:3]
         ^
SyntaxError: invalid syntax

?

Ref: https://jakevdp.github.io/PythonDataScienceHandbook/01.05-ipython-and-shell-commands.html

答案1

得分: 1

我得出了与Azeem相同的结论,即似乎没有直接的方法来做到这一点,因为iPython似乎只支持使用=进行shell输出的直接赋值。在iPython中,l = !ls | head -n 3 可能是一行中最好和最可读的方法,但这里有一个在任何地方都可以工作的Python替代方法:

import subprocess
l = subprocess.run(['ls'], stdout=subprocess.PIPE).stdout.decode('utf-8').splitlines()[:3]
英文:

I came to the same conclusion as Azeem, that there is no direct way to do this since iPython only seems to support direct assignment of shell output with =. l = !ls | head -n 3 is the probably the best and most readable way to do this in one line in iPython, but here's a Python alternative that will work anywhere:

import subprocess
l = subprocess.run(['ls'], stdout=subprocess.PIPE).stdout.decode('utf-8').splitlines()[:3]

答案2

得分: 1

There doesn't seem to exist a direct way to achieve that in one line under IPython.

According to that blog post:

anything appearing after ! on a line will be executed not by the Python kernel, but by the system command-line

and, IPython's System shell access:

Any input line beginning with a ! character is passed verbatim (minus the !, of course) to the underlying operating system.

Except for the variables in curly braces that are expanded before the command is passed to the underlying OS shell.


One possible solution could be to leverage the shell head command for this:

ls | head -n 3

and, in IPython:

files = !ls | head -n 3

Another alternative could be to resort to Python solutions such as os.listdir():

import os
files = [e for e in os.listdir() if not e.startswith('.') and os.path.isfile(e) ][:3]

or, glob.glob():

import glob
files = glob.glob('*')[:3]
英文:

There doesn't seem to exist a direct way to achieve that in one line under IPython.

According to that blog post:

> anything appearing after ! on a line will be executed not by the Python kernel, but by the system command-line

and, IPython's System shell access:

> Any input line beginning with a ! character is passed verbatim (minus the !, of course) to the underlying operating system.

Except for the variables in curly braces that are expanded before the command is passed to the underlying OS shell.


One possible solution could be to leverage the shell head command for this:

ls | head -n 3

and, in IPython:

files = !ls | head -n 3

Another alternative could be to resort to Python solutions such as os.listdir():

import os
files = [e for e in os.listdir() if not e.startswith('.') and os.path.isfile(e) ][:3]

or, glob.glob():

import glob
files = glob.glob('*')[:3]

huangapple
  • 本文由 发表于 2023年2月18日 14:29:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491605.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定