【问题标题】:AttributeError: 'tuple' object has no attribute 'write' error while writing into a fileAttributeError:“元组”对象在写入文件时没有属性“写入”错误
【发布时间】:2017-11-27 08:22:25
【问题描述】:
from netmiko import ConnectHandler      
from textfsm import * 

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

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

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

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

data = re_table.ParseText(output)

print (output)

for s in re_table.header:

          fo.write("%s;" %s)

fo.write("\n")

for row in data:
        print (row)
        for s in row:

                fo.write("%s" %s)
                fo.write("\n")

fo.close()

有人可以帮忙,关于以下错误:

回溯(最近一次通话最后): 文件“/Users/gtomy200/Desktop/Py/test.py”,第 20 行,在 fo.write("%s;" %s) AttributeError:“元组”对象没有“写入”属性

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    你想确保你open文件:

    fo = open("testme.txt" , 'w')
    #    ^^^^
    

    因为你正试图写入一个二元组:

    fo = ("testme.txt", 'w')
    #    ^ no open
    

    这是行不通的。

    【讨论】:

      【解决方案2】:

      fo 是一个元组,使用with open() 进行文件操作。更安全、更轻松。

      with open ("myfile.txt","w") as ff:
          ff.write("string") #you can't use anything but strings in here, 
                             #so convert your variables to string
      

      【讨论】:

        猜你喜欢
        • 2021-03-20
        • 2012-04-28
        • 2018-09-06
        • 1970-01-01
        • 2018-05-26
        • 2020-11-19
        • 2013-03-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多