如何通过命令行将json传递给python脚本?

2024-09-29 01:19:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我想通过命令行上的json将一组简单的名称/值对传递给python脚本。在

$ curl -s https://jsonplaceholder.typicode.com/posts/1 | python3.6 -c 'import sys, json; print json.load(sys.stdin)'
  File "<string>", line 1
    import sys, json; print json.load(sys.stdin)
                               ^
SyntaxError: invalid syntax
(23) Failed writing body

json很简单:

^{pr2}$

目标是分配变量,如:

if json.load(sys.stdin)["title"] is not None:
    post_title = json.load(sys.stdin)["title"]

我怎样才能正确地完成这个任务?在


Tags: 命令行httpsimport脚本名称comjsontitle