在Jupyter Notebook Python Cell
中运行shell命令时,如下所示:
output = ! some-shell-command
发射到标准输出(stdout
)的每一行都被捕获到一个类似于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
中的本地解决方案
谢谢大家!
对于这里希望将类型为
SList
的jupyter标准输出(stdout
)转换为JSON
的人,我就是这么做的:这假设
command
已将要处理到python字典中的输出适当格式化SList
有许多属性,它们以各种形式返回它:https://gist.github.com/parente/b6ee0efe141822dfa18b6feeda0a45e5
作为列表
作为换行符分隔字符串:
以空格分隔:
它的文件
使用
join()
将迭代器组合成字符串相关问题 更多 >
编程相关推荐