Jupyter / IPython SList :: 从 shell 执行运算符“!”获取非标记化输出
Jupyter / IPython SList :: Obtaining non-tokenized output from the shell execute operator "!"
当 shell 命令是 运行 在 Jupyter Notebook Python Cell
中时,像这样:
output = ! some-shell-command
发送到标准输出 (stdout
) 的每一行都在 list
like IPython
数据结构中捕获,称为 SList
。例如:
output = !echo -e 'line1\nline2\nline3'
print(output) # A IPython SList data-structure.
['line1', 'line2', 'line3']
然而,有时您希望保留原始字符串输出格式,而不是将标记化为列表,如下所示:
print(output)
line1
line2
line3
示例:对于结构化 JSON 输出——这是一个包含许多换行符的字符串——你不希望发生这种标记化.
那么,如何使用 !
运算符在 Jupyter Notebooks
中执行 shell 命令,并检索未标记化的输出(如上)?
理想情况下,解决方案应该是 Jupyter Notebooks
中的原生内容。
谢谢!
使用join()
将一个迭代器组合成一个字符串。
"\n".join(output)
SList
有很多属性,即return它有多种形式:
https://gist.github.com/parente/b6ee0efe141822dfa18b6feeda0a45e5
In [151]: ret = !ls *.json
In [152]: ret
Out[152]: ['foo1.json', 'foo.json', 'logins.json', 'stack56532806.json']
作为列表
In [153]: ret.l
Out[153]: ['foo1.json', 'foo.json', 'logins.json', 'stack56532806.json']
作为换行符分隔的字符串:
In [154]: ret.n
Out[154]: 'foo1.json\nfoo.json\nlogins.json\nstack56532806.json'
作为space分隔:
In [155]: ret.s
Out[155]: 'foo1.json foo.json logins.json stack56532806.json'
In [156]: type(ret)
它的文档
In [158]: ret?
Type: SList
String form: ['foo1.json', 'foo.json', 'logins.json', 'stack56532806.json']
Length: 4
File: /usr/local/lib/python3.6/dist-packages/IPython/utils/text.py
Docstring:
List derivative with a special access attributes.
These are normal lists, but with the special attributes:
* .l (or .list) : value as list (the list itself).
* .n (or .nlstr): value as a string, joined on newlines.
* .s (or .spstr): value as a string, joined on spaces.
* .p (or .paths): list of path objects (requires path.py package)
Any values which require transformations are computed only once and
cached.
对于那些希望将 SList
类型的 jupyter 标准输出 (stdout
) 转换为 JSON
的人,这就是我所做的:
import json
output = !command #output is an SList
j_out = json.dumps(output) #jout is a str
out_dict = json.loads(j_out) #out_dict is a list
out_dict[i][j]... #to access list elements
这假定 command
具有适当格式的输出以处理成 python 字典。
当 shell 命令是 运行 在 Jupyter Notebook Python Cell
中时,像这样:
output = ! some-shell-command
发送到标准输出 (stdout
) 的每一行都在 list
like IPython
数据结构中捕获,称为 SList
。例如:
output = !echo -e 'line1\nline2\nline3'
print(output) # A IPython SList data-structure.
['line1', 'line2', 'line3']
然而,有时您希望保留原始字符串输出格式,而不是将标记化为列表,如下所示:
print(output)
line1
line2
line3
示例:对于结构化 JSON 输出——这是一个包含许多换行符的字符串——你不希望发生这种标记化.
那么,如何使用 !
运算符在 Jupyter Notebooks
中执行 shell 命令,并检索未标记化的输出(如上)?
理想情况下,解决方案应该是 Jupyter Notebooks
中的原生内容。
谢谢!
使用join()
将一个迭代器组合成一个字符串。
"\n".join(output)
SList
有很多属性,即return它有多种形式:
https://gist.github.com/parente/b6ee0efe141822dfa18b6feeda0a45e5
In [151]: ret = !ls *.json
In [152]: ret
Out[152]: ['foo1.json', 'foo.json', 'logins.json', 'stack56532806.json']
作为列表
In [153]: ret.l
Out[153]: ['foo1.json', 'foo.json', 'logins.json', 'stack56532806.json']
作为换行符分隔的字符串:
In [154]: ret.n
Out[154]: 'foo1.json\nfoo.json\nlogins.json\nstack56532806.json'
作为space分隔:
In [155]: ret.s
Out[155]: 'foo1.json foo.json logins.json stack56532806.json'
In [156]: type(ret)
它的文档
In [158]: ret?
Type: SList
String form: ['foo1.json', 'foo.json', 'logins.json', 'stack56532806.json']
Length: 4
File: /usr/local/lib/python3.6/dist-packages/IPython/utils/text.py
Docstring:
List derivative with a special access attributes.
These are normal lists, but with the special attributes:
* .l (or .list) : value as list (the list itself).
* .n (or .nlstr): value as a string, joined on newlines.
* .s (or .spstr): value as a string, joined on spaces.
* .p (or .paths): list of path objects (requires path.py package)
Any values which require transformations are computed only once and
cached.
对于那些希望将 SList
类型的 jupyter 标准输出 (stdout
) 转换为 JSON
的人,这就是我所做的:
import json
output = !command #output is an SList
j_out = json.dumps(output) #jout is a str
out_dict = json.loads(j_out) #out_dict is a list
out_dict[i][j]... #to access list elements
这假定 command
具有适当格式的输出以处理成 python 字典。