2

我正在尝试使用textfsm使用正则表达式的模块,并将数据导入文本文件。下面是我的代码:

from netmiko import ConnectHandler      
from textfsm import *  

cisco_device = { 'device_type' : 'cisco_ios', 'ip' : 'x.x.x.x', 'username':'****0', 'password':'***9'}
net_connect = ConnectHandler(**cisco_device)

fo=("test.txt" , 'w')

output = net_connect.send_command("show ip int brief")

re_table = TextFSM('xr_show_int_br','r')     

data = re_table.ParseText(output)

print (output)
print(re_table.header)

for test in (re_table.header):
          fo.write(test)

fo.write("\n")

for row in data:
          for temp_row in data:
              fo.write(temp_row)
          fo.write("\n")


fo.close

但我收到了这个错误:

回溯(最后一次调用):文件“/Users/gtomy200/Desktop/Py/test.py”,第 11 行,在 re_table = TextFSM('xr_show_int_br','r') 文件“/Library/Frameworks/Python.framework /Versions/3.4/lib/python3.4/site-packages/textfsm.py",第 549 行,在init template.seek(0) AttributeError: 'str' 对象没有属性 'seek'

4

2 回答 2

1

遇到了同样的错误。打开文件为我解决了它。

with open('xr_show_int_br.txtfsm', 'r') as template:
    re_table = TextFSM(template)
于 2020-08-07T20:39:34.540 回答
0

似乎 xr_show_int_br 需要是一个文件对象。你在这里得到错误

于 2017-06-24T18:31:50.350 回答