p = subprocess.Popen(args = "myprog.exe" + " " +
str(input1) + " " +
str(input2) + " " +
str(input3) + " " +
strpoints, stdout = subprocess.PIPE)
在上面的代码中,input1、input2和input3都是被转换成字符串的整数。变量“strpoints”是任意长度字符串的列表。input1告诉myprog strpoints的长度。当然,当我尝试运行上述代码时,会收到以下错误消息:
TypeError: Can't convert 'list' object to str implicitly
如何将strpoints的所有元素传递给myprog.exe?我注定要做str(strpoints)然后myprog.exe用撇号等等来分析这个。?e、 g
`>>> x = ['a', 'b']
`>>> str(x)
"['a', 'b']"
或者我应该事先创造一个巨大的字符串?e、 g
'>>> x = ['a', 'b']
'>>> stringify(x)
' a b'
stringify应该是
def stringify(strlist):
rlist = ""
for i in strlist:
rlist = rlist + i + " "
return rlist
避免使用该字符串将所有参数连接到一个字符串中。在
只传递一个参数序列(列表或元组)就简单、更好、更安全。如果任何参数包含空格字符(这在文件名中很常见),则尤其如此。在
尝试使用字符串.连接公司名称:
args可以是一个序列:
如果参数包含shell元字符(例如“*”),并且您不希望它们被解释为shell元字符,则这一点更为正确。在
相关问题 更多 >
编程相关推荐