2019-06-15 巡检结果输出到文本

上次写了用Python做设备巡检2019-06-14 Python Paramiko模块 在Huawei OLT巡检实例
那么,输出的数据怎么处理?
举例,在每台OLT上输出数据类似下面这样:

ol01.xxx.lon#display ont info 0 all | no-more | include 48575443E8E0BD9D
{ <cr>||<K> }:

  Command:
          display ont info 0 all
  It will take a long time if the content you search is too much or the string you input is too long,
you can press CTRL_C to break
  0/ 1/0    2  48575443E8E0BD9D  active      online   normal   match    no

我只想要最后一行,就是

0/ 1/0    2  48575443E8E0BD9D  active      online   normal   match    no

写个函数来处理:

#定义示例字符串
st = '''ol01.xxx.lon#
display ont info 0 all | no-more | include 48575443E8E0B89D 
{ <cr>||<K> }:  

  Command:
          display ont info 0 all 
  It will take a long time if the content you search is too much or the string 
you input is too long, you can press CTRL_C to break
  0/ 1/0   18  48575443E8E0B89D  active      online   normal   match    no 
'''
def find_status(inp):
    for line in inp.split('\n'):
        if not 'active' in line:
            continue
        print(line)
        break
        return(line)
find_status(st)

这样输出的就是最后一行我要的数据。
再写一个for循环,把输出数据追加到一个文本里面。

 filename = "All_result.txt"
        with open(filename, "a") as f:
            f.write(find_status(Result))
            f.write('\n')

这样,运行完之后就会产生一个内容类似这样的文本:

0/ 2/1   12  48575443E8E0C79D  active      online   normal   match    no
0/ 2/1   47  48575443E8E12E9D  active      online   normal   match    no
0/ 2/1   48  48575443E8E1659D  active      online   normal   match    no
0/ 2/1   49  48575443E8E0A99D  active      online   normal   match    no
0/ 2/1   65  48575443E8E0AA9D  active      online   normal   match    no
.....
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • # Python 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列...
    小迈克阅读 8,178评论 1 3
  • 一、Python简介和环境搭建以及pip的安装 4课时实验课主要内容 【Python简介】: Python 是一个...
    _小老虎_阅读 11,325评论 0 10
  • 原文链接 《Python数据分析》(Python for Data Analysis, 2nd Edition)第...
    李绍俊阅读 12,752评论 0 5
  • 又是好几天没写日志了,不知道怎么总感觉心里怪难受的,没有坚持做到想要的那个自己,心里总有说不出来的滋味.........
    TA79晓英阅读 2,929评论 10 3
  • 雨之前 蜻蜓将云拉下来 点上一滴滴墨色 晕开 蜻蜓是雨的预言家 风也吹不散的 空气潮湿得可以让鱼也游弋其中 还是飞高一点
    长耳当听阅读 1,576评论 0 0

友情链接更多精彩内容