3 回答
TA贡献1829条经验 获得超7个赞
有SList许多属性,以各种形式返回它:
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'
以空格分隔:
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许多属性,以各种形式返回它:
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'
以空格分隔:
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.
TA贡献1845条经验 获得超8个赞
对于那些希望将类型为jupyter 标准输出 ( stdout) 的人来说,这就是我所做的:SListJSON
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 字典。
添加回答
举报