我使用下面的脚本将一个列表变量传递给另一个python脚本。这里,我将a作为列表变量传递。
脚本test.py:
import sys,os
a=[1,2,3,4,5]
os.system(f"python test2.py '{a}' ")下面的脚本将接收变量并尝试打印传递给test2.py的内容
脚本test2.py:
import sys
print("inside test4.py")
received_list=sys.argv
print(received_list)
print(sys.argv[0])
print(sys.argv[1])我试图获取完整的列表,但是我只得到了像这个'[1,这样的第一个元素
实际输出:
inside test2.py
['test2.py', "'[1,", '2,', '3,', '4,', "5]'"]
test2.py
'[1,预期输出:
inside test2.py
['test2.py', '[1,2,3,4,5]']
test2.py
[1,2,3,4,5]如果我显式地传递列表而不是变量即os.system(f'python test4.py "[1,2,3,4,5]"'),我就能够得到预期的输出
如何通过传递变量来实现这一点。
发布于 2019-11-29 05:45:33
test.py
import sys,os
a=[1,2,3,4,5]
os.system("python test4.py "+",".join(map(str,a)))test4.py
import sys, ast
print("inside test4.py")
received_list=sys.argv
print(received_list)
print(sys.argv[0])
print(list(ast.literal_eval(sys.argv[1])))https://stackoverflow.com/questions/59099266
复制相似问题