我是python3的新手,正在尝试将Python2.7代码转换为python3,因此我遇到了这个问题。让我知道我哪里错了。
for n in range (755,767):
tn.write(b"vlan " + str(n) + "\n")
tn.write(b"name Python_VLAN_" + str(n) + "\n")
错误:
Traceback (most recent call last):
File "./telnetlib_vlan_loop.py", line 30, in <module>
tn.write(b"vlan " + str(n) + "\n")
TypeError: can't concat str to bytes**strong text**
发布于 2019-07-14 04:56:06
错误状态:TypeError: can't concat str to bytes
您当前的问题是,您有bytes
和str
,而您正试图将它们添加到一起。在此之前,您需要使用相同的类型。
如果需要编写为str
或bytes
,则不提供
如果使用bytes
,请将代码更改为:
for n in range (755,767):
tn.write("vlan {}\n".format(n).encode())
tn.write("name Python_VLAN_{}\n".format(n).encode())
如果str
只是删除encode
for n in range (755,767):
tn.write("vlan {}\n".format(n))
tn.write("name Python_VLAN_{}\n".format(n))
更新:修复了格式的拼写
发布于 2020-06-06 00:15:32
for n in range(755,767):
tn.write(b"vlan " + str(n).encode('ascii') + b"\n")
tn.write(b"name Python_VLAN_" + str(n).encode('ascii') + b"\n")
这可能会对trick...hope有所帮助
发布于 2020-06-16 03:00:35
我处理了同样的问题,并用上面的代码在两者中用.enconde('ascii')修复了这个问题
https://stackoverflow.com/questions/57022625
复制相似问题