首页 > 解决方案 > 如何将从模块处理的数据摄取到python3中的文件中

问题描述

我正在寻找将从模块处理的数据摄取到python中的文件中。我创建了一个脚本来收集数据并将其放入目录中,然后另一个模块子集从那里获取数据并最终解析所需的数据。

1) DataCollecter.py,收集数据 2) processData.py,读取数据并从整个数据集中获取所需的数据 3) ptsrch.py​​,该模块有助于选择要从收集processData的值中选择的值。keyDataset

现在我期待将所有数据放入一个文件中,并processData.py在它出现在屏幕上时进行处理..

任何有助于解决此问题的建议和想法都将受到高度赞赏。

进程数据.py

import os
import ptsrch
import command

directory_list = list()
res = command.data()
for root, dirs, files in os.walk("/home/data/plura/Test/NWK", topdown=False):
    for name in dirs:
        directory_list.append(os.path.join(root, name))

for key in res.keys():
    print('\x1b[0;33;40m' + "Start of the Report %s" %(key) + '\x1b[0m')
    print('\x1b[0;33;40m' + '-' * 120 + '\x1b[0m')
    dir = '/home/data/plura/Test/NWK/'+key
    ptsrch.data_process(dir,res[key])
    print("")
    print("")

ptsrch.py

import os
def data_process(dir, keyword):
    files = list(os.listdir(dir))
    for file in files:
        server_name = file.split('.')[0]
        open_dir = dir+'/'+file
        with open(open_dir, 'r') as f:
            data = f.readlines()
            for line in data:
                if keyword in line:
                    print("%s %s" %(server_name, line.strip()))

当前数据来自processData.py

Start of the Report show-ip-int-brief
------------------------------------------------------------------------------------------------------------------------
hummer-stvdx-sw Port-channel 49                unassigned        administratively down     down
hummer-stvdx-sw TenGigabitEthernet 50/0/7      unassigned        administratively down     down
hummer-stvdx-sw TenGigabitEthernet 50/0/8      unassigned        administratively down     down
hummer-stvdx-sw TenGigabitEthernet 50/0/41     unassigned        administratively down     down
hummer-stvdx-sw TenGigabitEthernet 50/0/43     unassigned        administratively down     down
hummer-stvdx-sw TenGigabitEthernet 50/0/44     unassigned        administratively down     down
hummer-stvdx-sw TenGigabitEthernet 50/0/45     unassigned        administratively down     down
hummer-stvdx-sw TenGigabitEthernet 50/0/46     unassigned        administratively down     down
hummer-stvdx-sw TenGigabitEthernet 50/0/47     unassigned        administratively down     down
hummer-stvdx-sw TenGigabitEthernet 50/0/48     unassigned        administratively down     down
hummer-stvdx-sw TenGigabitEthernet 50/0/51     unassigned        administratively down     down


Start of the Report show-logging-raslog
------------------------------------------------------------------------------------------------------------------------
hummer-stvdx-sw 2018/02/26-07:37:55, [EM-1034], 284492,, ERROR, VDX6730-DC, PS 2 is set to faulty, rc=2000e.
hummer-stvdx-sw 2018/04/16-10:28:52, [EM-1034], 284773,, ERROR, VDX6730-DC, PS 1 is set to faulty, rc=2000e.


Start of the Report show-vcs
------------------------------------------------------------------------------------------------------------------------


Start of the Report show-vcs-details
------------------------------------------------------------------------------------------------------------------------


Start of the Report show-version
------------------------------------------------------------------------------------------------------------------------
hummer-stvdx-sw NOS      v2.1.1a

标签: python-3.x

解决方案


推荐阅读