python-3.x - 如何将从模块处理的数据摄取到python3中的文件中
问题描述
我正在寻找将从模块处理的数据摄取到python中的文件中。我创建了一个脚本来收集数据并将其放入目录中,然后另一个模块子集从那里获取数据并最终解析所需的数据。
1) DataCollecter.py,收集数据 2) processData.py,读取数据并从整个数据集中获取所需的数据 3) ptsrch.py,该模块有助于选择要从收集processData
的值中选择的值。key
Dataset
现在我期待将所有数据放入一个文件中,并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 - 如何将控制台发送到手机的验证码放到一个pyqt5小部件中?
- javascript - 使用 Promise.all 嵌套 async/await
- javascript - Daterangepicker 在网页 ASP.NET MVC 中不起作用
- python-3.7 - Webscraper 没有使用 bs4 给出正确的结果
- java - 如何让这个 Java 代码格式化程序更漂亮
- mysql - Updating table by appending onto it several hundred rows
- javascript - Does calling setter of useState hook inside if statement imply BREAKING RULES OF HOOKS?
- excel - 有没有办法在我的可执行 (exe) 文件中嵌入一个 excel 文件?
- reactjs - 如何使用 nodemailer 更改邮件的发件人姓名?
- postgresql - 从 CSV 文件更新(同步)一个非常大的 PostgreSQL 表,而不重新创建表