[英]TypeError: can only concatenate tuple (not “str”)
我想在我们的设备中自动创建VLAN,但是我的脚本抛出此错误:
tn.write(cmd + "\\n") TypeError: can only concatenate tuple (not "str") to tuple.
vlannumbers = int(input("Enter the number for vlans :"))
for i in range(2,vlannumbers):
cmd = (("/cfg/l2/vlan "), i)
tn.write(cmd + "\n")
cmd1 = "apply"
tn.write(cmd1 + "\n")
print "ok"
tn.close()
cmd
在代码中保留元组值(不是字符串)。 例如:
>>> cmd = (("/cfg/l2/vlan "), 2)
>>> type(cmd)
<type 'tuple'> # <--- it's tuple
由于您要将其初始化为字符串,因此您应该执行以下操作:
cmd = "/cfg/l2/vlan {}".format(i)
字符串连接是string
类型的对象之间的连接。
您可以尝试以下方法:
vlannumbers = int(input("Enter the number for vlans :"))
cmd_base = "/cdg/l2/vlan"
for i in range(2,vlannumbers):
# cmd_base is not affected,
# since concatenated string value is stored in `cmd`
cmd = cmd_base + "%d"%i
tn.write(cmd + "\n")
cmd1 = "apply"
tn.write(cmd1 + "\n")
print "ok"
tn.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.