【问题标题】:Pipe the output of a program as args into a Python script?将程序的输出作为 args 传递到 Python 脚本中?
【发布时间】:2026-02-05 22:55:02
【问题描述】:

我正在编写一个脚本来接收我的基础架构中一些 LXD 容器的 JSON 输出。 LXD 漂亮地打印了一些 JSON,我将把那个 JSON 输入到我的 Python 脚本中。

python myscript.py | lxc list --format json

我可以在我的脚本中做些什么来正确读取这些数据或让它处理这个管道输入?

尝试

import sys
for line in sys.stdin:
    print line

【问题讨论】:

标签: python json


【解决方案1】:

它是pipe from | pipe to,所以如果你想通过管道将 lxc 传递给 python,请将其切换到另一种方式

lxc list --format json | python myscript.py

【讨论】: