如何将 Python 列表转换为 shell 可读的字符串系列?
How to convert Python lists into a shell-readable series of strings?
我有这个 shell 脚本(由文件名 depends
调用)保存在我的 $PATH
:
#!/bin/bash
for i in "$@"
do
depends=+("nodejs-$i")
done
echo $depends
其设计采用的输入(即 $@
)采用示例格式:
'flatten' 'once' 'is-file' 'multistream' 'piece-length' 'junk' 'xtend' 'bencode' 'readable-stream' 'run-parallel 'filestream' 'simple-sha1' 'minimist' 'block-stream2'
。所以我希望能够编写 package.json
文件中列出的依赖项,该文件包含在此 Python 脚本的 DEP
列表变量中(文件名 npm2.py
):
import json
from sys import argv
print(argv[1])
from subprocess import call
with open("/home/fusion809/OBS/home:fusion809:arch_extra/nodejs-" + argv[1] + "/src/package/package.json") as json_file:
json_data = json.load(json_file)
deps = json_data["dependencies"]
LEN=len(deps)
print(LEN)
i=0
DEP=list()
print(DEP)
for key, value in deps.items():
print(key)
DEP.append(key)
i = i+1
print(i)
#call(["cpobsn", key, argv[1]])
print(DEP)
call("depends", DEP) # returns errors as DEP is a list!
到一个新变量,比如 DEPS
,这样它就可以用作 depends
shell 脚本的输入。如果这对我有帮助,我想将其转换为之前显示的标准输出的当前 DEP
变量:
['flatten', 'once', 'is-file', 'multistream', 'piece-length', 'junk', 'xtend', 'bencode', 'readable-stream', 'run-parallel', 'filestream', 'simple-sha1', 'minimist', 'block-stream2']
因此我来这里是想问一下如何将 Python 列表转换为 shell 可读的 space 分隔字符串集。
subprocess.call
将其参数放在一个列表中。尝试:
call(["depends"] + DEP)
我有这个 shell 脚本(由文件名 depends
调用)保存在我的 $PATH
:
#!/bin/bash
for i in "$@"
do
depends=+("nodejs-$i")
done
echo $depends
其设计采用的输入(即 $@
)采用示例格式:
'flatten' 'once' 'is-file' 'multistream' 'piece-length' 'junk' 'xtend' 'bencode' 'readable-stream' 'run-parallel 'filestream' 'simple-sha1' 'minimist' 'block-stream2'
。所以我希望能够编写 package.json
文件中列出的依赖项,该文件包含在此 Python 脚本的 DEP
列表变量中(文件名 npm2.py
):
import json
from sys import argv
print(argv[1])
from subprocess import call
with open("/home/fusion809/OBS/home:fusion809:arch_extra/nodejs-" + argv[1] + "/src/package/package.json") as json_file:
json_data = json.load(json_file)
deps = json_data["dependencies"]
LEN=len(deps)
print(LEN)
i=0
DEP=list()
print(DEP)
for key, value in deps.items():
print(key)
DEP.append(key)
i = i+1
print(i)
#call(["cpobsn", key, argv[1]])
print(DEP)
call("depends", DEP) # returns errors as DEP is a list!
到一个新变量,比如 DEPS
,这样它就可以用作 depends
shell 脚本的输入。如果这对我有帮助,我想将其转换为之前显示的标准输出的当前 DEP
变量:
['flatten', 'once', 'is-file', 'multistream', 'piece-length', 'junk', 'xtend', 'bencode', 'readable-stream', 'run-parallel', 'filestream', 'simple-sha1', 'minimist', 'block-stream2']
因此我来这里是想问一下如何将 Python 列表转换为 shell 可读的 space 分隔字符串集。
subprocess.call
将其参数放在一个列表中。尝试:
call(["depends"] + DEP)