Python 属性错误:';unicode';对象没有属性';读线';
我通过cisco设备与paramiko连接。我知道我可以用netmiko代替paramiko,但这不是我目前问题的焦点。无论如何,在执行命令“show run | I hostname\n”并将输出保存到变量“hostOutput”之前,我希望在行中搜索单词“hostname”。找到单词后,我想把整行打印出来Python 属性错误:';unicode';对象没有属性';读线';,python,python-3.x,paramiko,Python,Python 3.x,Paramiko,我通过cisco设备与paramiko连接。我知道我可以用netmiko代替paramiko,但这不是我目前问题的焦点。无论如何,在执行命令“show run | I hostname\n”并将输出保存到变量“hostOutput”之前,我希望在行中搜索单词“hostname”。找到单词后,我想把整行打印出来 def devInfo(): print ('') print ('[3] Collecting device informations...') print ('') ssh.slav
def devInfo():
print ('')
print ('[3] Collecting device informations...')
print ('')
ssh.slave.send('show run | i hostname\n')
time.sleep(1)
hostOutput = ssh.slave.recv(9999).decode('UTF-8')
lines = hostOutput.readlines()
for line in lines:
if re.search(r'hostname', line):
print line
break
devInfo()
但是我得到了异常AttributeError:“unicode”对象没有属性“readlines”。。。我做错了什么?如何修复unicode,使其成为str?只需键入cast
hostOutput=ssh.slave.recv(9999)。将('UTF-8')
解码为str,如下所示:
hostOutput = str(hostOutput)
lines = hostOutput
或
也许我现在有点困惑,但在我的示例中,如果没有打开文件,我就找不到问题所在…hostOutput已经是一个字符串。改为使用
lines=hostOutput.split('\n')
。为了让您了解这一点,文件的readlines()
函数的设计值得怀疑,因为使用一个文件或字符串的自由函数会更好。将其作为memberfunction会引发一个问题,为什么像unicode这样的字符串类(也可以拆分为行)没有一个。顺便说一句:作为这里的一个新用户,请阅读下面的内容。另外,对于实际代码的问题,请始终提取并提供一个。
lines = str(hostOutput).readlines()