我的代码如下:
if len(sys.argv)>1:
host = sys.argv[1]
number = sys.argv[2] if len(sys.argv)>2 else "1"
size = sys.argv[3] if len(sys.argv)>3 else "56"
timeout = sys.argv[4] if len(sys.argv)>4 else "1"
proc = subprocess.Popen(["ping \"" + host + "\" -c \"" + number + "\" -s \"" + size + "\" -W \"" + timeout + "\" 2>/dev/null | grep packets | sed 's/[^0-9,%]*//g'"], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
print "address=\"" + host + "\" data=" + out
我需要拆分列表。我怎么能做到这一点?
我尝试的所有内容都会导致错误:cannot concatenate 'str' and ... objects
就像我尝试的那样:
...
res=list(out)
print "address=\"" + host + "\" data=" + res
我收到了错误:
TypeError: cannot concatenate 'str' and 'list' objects
答案 0 :(得分:1)
要统一字符串和列表,首先必须从列表中删除字符串:
res=list(out)
print "address=\"" + str(host) + "\" data=" + str(res)