删除两个单词之间的所有字符并替换为空格

时间:2012-05-12 21:02:07

标签: python cisco

我正在使用Python 3.我已经编写了两个程序。一个循环通过csv文件获取Cisco交换机的IP地址,登录,运行命令,并将结果输出到单个文本文件。所以我最终得到了一些文本文件......每个交换机一个。第二个程序使用xlwt将每个文本文件中的信息写入Excel中自己的工作表。

主要想法是我需要开发一个报告,显示进出服务的端口。一旦我将这些导入到Excel中,我就可以编写一些公式来提取我需要的数据。但是现在当我将它导入Excel时,我必须手动删除一些单元格,因为一切都没有排列,因为名称列中某些单词之间的空格(我以空格分隔的方式导入Excel。我试图用字符串和列表方法(拆分,连接,切片等)做一些事情,但我无法完全得到我想要的东西。并且Name列在任何类型的一致约定中都没有标准化。我注意到虽然名称实际上可能很长,但它会被截断为一定数量的字符。

理想情况下,删除前4行(最顶部有空白行)和最后一行,然后在端口和状态之间删除所有内容(全部删除包括标题的列)。

这是从交换机获取数据后文件的外观。

sw1#term length 0
sw1#show interfaces status

Port      Name               Status       Vlan       Duplex  Speed Type
Gi0/1     Trunk to switch (a connected    1          a-full  a-100 10/100/1000BaseTX
Gi0/2     Network augment pe connected    1          a-full a-1000 10/100/1000BaseTX
Gi0/3                        connected    1          a-full a-1000 10/100/1000BaseTX
Gi0/4                        connected    1          a-full  a-100 10/100/1000BaseTX
Gi0/5                        notconnect   1            auto   auto Not Present
Gi0/6                        notconnect   1            auto   auto Not Present
Gi0/7                        notconnect   1            auto   auto Not Present
Gi0/8                        notconnect   1            auto   auto Not Present
Gi0/9                        notconnect   1            auto   auto Not Present
Gi0/10                       connected    1          a-full  a-100 10/100/1000BaseTX
Gi0/11                       notconnect   1            auto   auto Not Present
Gi0/12                       connected    1          a-full  a-100 10/100/1000BaseTX
Gi0/13                       disabled     1            auto   auto Not Present
Gi0/14                       disabled     1            auto   auto Not Present
Gi0/15                       disabled     1            auto   auto Not Present
Gi0/16                       disabled     1            auto   auto Not Present
sw1#logout

最终结果我想在下面。这应该允许行/列结构在导入Excel时保持不变。请注意,所有列信息都以空格分隔。我发现以固定宽度导入或用空格分隔处理连续空格作为一个检查似乎工作得很好。

Port      Status       Vlan       Duplex  Speed Type
Gi0/1     connected    1          a-full  a-100 10/100/1000BaseTX
Gi0/2     connected    1          a-full a-1000 10/100/1000BaseTX
Gi0/3     connected    1          a-full a-1000 10/100/1000BaseTX
Gi0/4     connected    1          a-full  a-100 10/100/1000BaseTX
Gi0/5     notconnect   1            auto   auto Not Present
Gi0/6     notconnect   1            auto   auto Not Present
Gi0/7     notconnect   1            auto   auto Not Present
Gi0/8     notconnect   1            auto   auto Not Present
Gi0/9     notconnect   1            auto   auto Not Present
Gi0/10    connected    1          a-full  a-100 10/100/1000BaseTX
Gi0/11    notconnect   1            auto   auto Not Present
Gi0/12    connected    1          a-full  a-100 10/100/1000BaseTX
Gi0/13    disabled     1            auto   auto Not Present
Gi0/14    disabled     1            auto   auto Not Present
Gi0/15    disabled     1            auto   auto Not Present
Gi0/16    disabled     1            auto   auto Not Present

任何指针都将不胜感激。我认为正则表达式可能正常,但我需要一些帮助来构建它。我希望这不是太模糊。

删除了以前的更新并将其移至新的线程

4 个答案:

答案 0 :(得分:1)

首先,下面的代码删除了name-column。从那里拿出它并添加一个不错的csv打印输出。提示:column[-1]是最后一个条目,column[-2]是倒数第二个。如果字符串Not存在,请加入这些......

#!/usr/bin/env python

tokens = ['connected', 'notconnect', 'disabled']

with open('text') as fd:
    for line in fd:
        line = line.strip().split()

        connection = [line[0]]
        found = False

        for i in line:
            if i in tokens:
                found = True
            if found:
                connection.append(i)

        print connection

输出:

['Gi0/1', 'connected', '1', 'a-full', 'a-100', '10/100/1000BaseTX']
['Gi0/2', 'connected', '1', 'a-full', 'a-1000', '10/100/1000BaseTX']
['Gi0/3', 'connected', '1', 'a-full', 'a-1000', '10/100/1000BaseTX']
['Gi0/4', 'connected', '1', 'a-full', 'a-100', '10/100/1000BaseTX']
['Gi0/5', 'notconnect', '1', 'auto', 'auto', 'Not', 'Present']
['Gi0/6', 'notconnect', '1', 'auto', 'auto', 'Not', 'Present']
['Gi0/7', 'notconnect', '1', 'auto', 'auto', 'Not', 'Present']
['Gi0/8', 'notconnect', '1', 'auto', 'auto', 'Not', 'Present']
['Gi0/9', 'notconnect', '1', 'auto', 'auto', 'Not', 'Present']
['Gi0/10', 'connected', '1', 'a-full', 'a-100', '10/100/1000BaseTX']
['Gi0/11', 'notconnect', '1', 'auto', 'auto', 'Not', 'Present']
['Gi0/12', 'connected', '1', 'a-full', 'a-100', '10/100/1000BaseTX']
['Gi0/13', 'disabled', '1', 'auto', 'auto', 'Not', 'Present']
['Gi0/14', 'disabled', '1', 'auto', 'auto', 'Not', 'Present']
['Gi0/15', 'disabled', '1', 'auto', 'auto', 'Not', 'Present']
['Gi0/16', 'disabled', '1', 'auto', 'auto', 'Not', 'Present']

评论后更新:

这就是我使用KISS principle

的方式
#!/usr/bin/env python

import sys

tokens = ['connected', 'notconnect', 'disabled']

with open('text') as fd:
    for line in fd:
        line = line.strip().split()

        connection = [line[0]]
        found = False

        for i in line:
            if i in tokens:
                found = True
            if found:
                connection.append(i)

        if 'Not' in connection and 'Present' in connection:
            # Remove last 2 entries
            connection.pop() ; connection.pop()
            connection.append('Not Present')

        print connection

输出:

['Gi0/1', 'connected', '1', 'a-full', 'a-100', '10/100/1000BaseTX']
['Gi0/2', 'connected', '1', 'a-full', 'a-1000', '10/100/1000BaseTX']
['Gi0/3', 'connected', '1', 'a-full', 'a-1000', '10/100/1000BaseTX']
['Gi0/4', 'connected', '1', 'a-full', 'a-100', '10/100/1000BaseTX']
['Gi0/5', 'notconnect', '1', 'auto', 'auto', 'Not Present']
['Gi0/6', 'notconnect', '1', 'auto', 'auto', 'Not Present']
['Gi0/7', 'notconnect', '1', 'auto', 'auto', 'Not Present']
['Gi0/8', 'notconnect', '1', 'auto', 'auto', 'Not Present']
['Gi0/9', 'notconnect', '1', 'auto', 'auto', 'Not Present']
['Gi0/10', 'connected', '1', 'a-full', 'a-100', '10/100/1000BaseTX']
['Gi0/11', 'notconnect', '1', 'auto', 'auto', 'Not Present']
['Gi0/12', 'connected', '1', 'a-full', 'a-100', '10/100/1000BaseTX']
['Gi0/13', 'disabled', '1', 'auto', 'auto', 'Not Present']
['Gi0/14', 'disabled', '1', 'auto', 'auto', 'Not Present']
['Gi0/15', 'disabled', '1', 'auto', 'auto', 'Not Present']
['Gi0/16', 'disabled', '1', 'auto', 'auto', 'Not Present']

答案 1 :(得分:1)

with open('file') as f:
    lines = f.readlines()
    lines = lines[-1:] + lines[2:-1]
    for line in lines:
        print line[0:11] + line[35:-1]

我认为这将大致按照你的意愿行事;你可能需要稍微玩一些数字,因为我自己没有运行它。所有它使用的是列表(或字符串)索引:

  • list [x:]是从x开始的所有条目
  • list [x:y]是从x到y的所有条目
  • list [-x]是结尾的第x行

lines[-1:] + lines[2:-1]将最后一行放在第一位,扔掉前两行; line[0:11] + line[35:-1]排除了您不想要的部分和最终换行符。

更新如果您想要写入新文件而不是stdout:

with open('infile') as in:
    with open('outfile', 'w') as out:
        lines = in.readlines()
        ...
            print(line[0:6] + line[28:-1], file=out)

事实上,由于readlines一次读取所有内容,您可以这样做:

with open('infile') as in:
    lines = in.readlines()
with open('outfile', 'w') as out:
    for line in lines:
        ....
        print(line[0:6] + line[28:-1], file=out)

因为不需要打开输入文件(当with完成时它已关闭。)

答案 2 :(得分:1)

我跳过前3行:

sw1#term length 0
sw1#show interfaces status

<强>程序:

with open('in.txt') as f,open('out.txt','w') as out:
   line1=f.readline()
   ind1=line1.find('Name')
   ind2=line1.find('Status')
   x=line1.split()
   x.remove('Name')
   y="%-13s %-15s %-6s %-7s %-8s %-s"%(x[0],x[1],x[2],x[3],x[4],x[5])
   out.write(y+'\n')
   for x in f:
       x=x[:ind1]+x[ind2:]
       x=x.split()
       y="%-13s %-15s %-6s %-7s %-8s %-s"%(x[0],x[1],x[2],x[3],x[4],x[5])
       out.write(y+'\n')


Port          Status          Vlan   Duplex  Speed    Type
Gi0/1         connected       1      a-full  a-100    10/100/1000BaseTX
Gi0/2         connected       1      a-full  a-1000   10/100/1000BaseTX
Gi0/3         connected       1      a-full  a-1000   10/100/1000BaseTX
Gi0/4         connected       1      a-full  a-100    10/100/1000BaseTX
Gi0/5         notconnect      1      auto    auto     Not
Gi0/6         notconnect      1      auto    auto     Not
Gi0/7         notconnect      1      auto    auto     Not
Gi0/8         notconnect      1      auto    auto     Not
Gi0/9         notconnect      1      auto    auto     Not
Gi0/10        connected       1      a-full  a-100    10/100/1000BaseTX
Gi0/11        notconnect      1      auto    auto     Not
Gi0/12        connected       1      a-full  a-100    10/100/1000BaseTX
Gi0/13        disabled        1      auto    auto     Not
Gi0/14        disabled        1      auto    auto     Not
Gi0/15        disabled        1      auto    auto     Not
Gi0/16        disabled        1      auto    auto     Not

答案 3 :(得分:0)

如果类型不存在,我们只取第一个和最后5个值并做一个小例子切换:

print "{:10} {:15} {:5} {:6} {:6} {}".format("port", "status", "vlan", "duplex", "speed", "type")
with open(my_filename) as logfile:
    content = logfile.read()
    for line in content.splitlines()[4:]:
        port = line.split()[0]
        if line.strip().endswith("Not Present"):
            itype = "Not Present"
            status, vlan, duplex, speed = line.split()[-6:-2]
        else:
            status, vlan, duplex, speed, itype = line.split()[-5:]
        print "{:10} {:15} {:5} {:6} {:6} {}".format(port, status, vlan, duplex, speed, itype)

收率:

port       status          vlan  duplex speed  type            
Gi0/1      connected       1     a-full a-100  10/100/1000BaseTX
Gi0/2      connected       1     a-full a-1000 10/100/1000BaseTX
Gi0/3      connected       1     a-full a-1000 10/100/1000BaseTX
Gi0/4      connected       1     a-full a-100  10/100/1000BaseTX
Gi0/5      notconnect      1     auto   auto   Not Present
Gi0/6      notconnect      1     auto   auto   Not Present
Gi0/7      notconnect      1     auto   auto   Not Present
Gi0/8      notconnect      1     auto   auto   Not Present
Gi0/9      notconnect      1     auto   auto   Not Present
Gi0/10     connected       1     a-full a-100  10/100/1000BaseTX
Gi0/11     notconnect      1     auto   auto   Not Present
Gi0/12     connected       1     a-full a-100  10/100/1000BaseTX
Gi0/13     disabled        1     auto   auto   Not Present
Gi0/14     disabled        1     auto   auto   Not Present
Gi0/15     disabled        1     auto   auto   Not Present
Gi0/16     disabled        1     auto   auto   Not Present