繁体   English   中英

使用 exec 在 javascript 中使用 args 运行 python 脚本

[英]Run python script with args in javascript with exec

我有纬度、经度和频率等输入。 我想在 JS 文件上运行这个命令:

exec(`python test.py '{"latitude":${latitude},"longitude":${longitude},"frequency":${frequency}}'`, (error, stdout, stderr) => { .... })

但是当我运行时出现以下错误:

error: Command failed: python test.py '{"latitude":48.118097,"longitude":-1.636503,"frequency":2.4}'
Traceback (most recent call last):
  File "test.py", line 16, in <module>
    data=json.loads(sys.argv[1])
  File "C:\Users\****\AppData\Local\Programs\Python\Python38\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "C:\Users\****\AppData\Local\Programs\Python\Python38\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\****\AppData\Local\Programs\Python\Python38\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

我的python脚本以

import sys
import json
data=json.loads(sys.argv[1])

此外,如果我只运行没有 args 的 python 脚本,并直接在 python 脚本中写入随机数据,它就可以工作。 所以我认为这是exec命令中写入的args的问题。

你能帮我么。

也许添加双引号?

python test.py '\"{"latitude":48.118097,"longitude":-1.636503,"frequency":2.4}\"'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM