如何将从模块处理的数据摄取到python3中的文件中

时间:2018-05-02 11:09:03

标签: python-3.x

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

1)DataCollecter.py,收集数据 2)processData.py,读取数据并从整个数据集中获取所需数据 3)ptsrch.py​​,此模块可帮助processData选择要从key收集的Dataset值。

现在我期待将所有数据放入一个文件中,从processData.py开始进入屏幕..

任何建议&我们非常感谢为此提供帮助的想法。

processData.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

0 个答案:

没有答案