我正在编写一个python脚本从文件中提取grep字符串,并以以下格式在csv文件中显示输出
输入文件(result_EPFT_config_device):
Hostname SIM-MPL-LTE-PE-RTR-134
loopback 22.13.7.34
lpts punt excessive-flow-trap
penalty-rate arp 10
penalty-rate icmp 50
penalty-rate igmp 50
penalty-rate ip 100
exclude interface Bundle-Ether6
exclude interface Bundle-Ether8
exclude interface Bundle-Ether15
exclude interface Bundle-Ether16
exclude interface Bundle-Ether53
exclude interface TenGigE0/0/1/1
exclude interface TenGigE0/1/1/0
exclude interface Bundle-Ether6.2
exclude interface Bundle-Ether6.4
exclude interface Bundle-Ether8.2
exclude interface Bundle-Ether8.4
exclude interface Bundle-Ether16.2
exclude interface Bundle-Ether16.4
exclude interface Bundle-Ether53.2
exclude interface TenGigE0/0/1/3.100
exclude interface TenGigE0/0/1/3.102
exclude interface TenGigE0/0/1/3.103
exclude interface TenGigE0/0/1/3.104
exclude interface TenGigE0/1/1/0.100
exclude interface GigabitEthernet0/0/0/1
exclude interface GigabitEthernet0/0/0/6
exclude interface GigabitEthernet0/0/0/9
dampening.
non-subscriber-interfaces
report-threshold 10
下面是我到目前为止准备的python脚本。仅能grep字符串并打印
import sys
import telnetlib
import os
import subprocess
import re
import csv
fh = open("result_EPFT_config_device", "r")
fh1 = open("testingAjay", "w+")
line = fh.readlines()
for lines in line:
if re.search("(lpts punt excessive-flow-trap)", lines):
m = (lines.split(' '))
print m[0], m[1], m[2]
if re.search("(penalty-rate arp)", lines):
n = (lines.split(' '))
print n[0], n[1], n[2]
if re.search("(penalty-rate icmp)", lines):
a = (lines.split(' '))
print a[0], a[1], a[2]
if re.search("(penalty-rate igmp)", lines):
b = (lines.split(' '))
print b[0], b[1], b[2]
if re.search("(penalty-rate ip)", lines):
c = (lines.split(' '))
print c[0], c[1], c[2]
if re.search("(dampening)", lines):
c = (lines.split(' '))
print c[0]
if re.search("(non-subscriber-interfaces)", lines):
c = (lines.split('-'))
print c[0], c[1], c[2]
if re.search("(report-threshold 10)", lines):
c = (lines.split(' '))
print c[0], c[1]
我的脚本输出:
lpts punt excessive-flow-trap
penalty-rate arp 10
penalty-rate icmp 50
penalty-rate igmp 50
penalty-rate ip 100
dampening.
non subscriber interfaces
report-threshold 10
现在在这里,我想将输出放入csv文件中,如下所示
Hostname|loopback|lpts punt excessive-flow-trap|penalty-rate arp|penalty-rate icmp|penalty-rate igmp|penalty-rate ip|dampening|non-subscriber-interfaces|report-threshold
SIM-MPL-LTE-PE-RTR-134|1.1.1.1|yes|10|50|50|100|Yes|Yes|10
NDL-MPL-PE-RTR-195|2.2.2.2|No|No|No|20|50|NO|20Yes
如上面的屏幕快照所示,如果 lpt spunt超出流量陷阱列必须标记为“ YES”(如果输入文件中存在),否则标记为“ NO”。类似的逻辑需要应用于列阻尼和非用户界面列
请您帮我实现上述要求的csv格式的输出要求
答案 0 :(得分:0)
我们在这里!因此,正如上面的评论所说,您可以只使用“ startswith”而不是regex来匹配行。
我在这里使用的是python3而不是python2。
如果您使用“ python3 main.py”在目录中运行此文件,它将在“ inputs”子目录中搜索所有要分析的文件。
然后,我们为每个包含相关字段的文件构建字典并加载其值。我们将这些字典添加到列表中。最后,我们只将标头写入csv,然后遍历各行并写入值。您可能可以在读取文件时写行,但是我发现在头脑中将解析和输出清理器分开。
当您要遍历行中的每一行时,我将“行中的行”的顺序更改为“行中的行”。
import os
import csv
def parseFile(fileName):
# We are using a dictionary to store info for each file
data = dict()
# Set all Yes/Nos to NO by default
data["lpts punt excessive-flow-trap"] = "NO"
data["dampening"] = "NO"
data["non-subscriber-interfaces"] = "NO"
fh = open(fileName, "r")
lines = fh.readlines()
for line in lines:
# We need this so we don't end up with newline characters in our CSV
line = line.rstrip("\n")
# We dont need regular expressions here as matching whole line
# Do YES/NO first
if line == "lpts punt excessive-flow-trap":
data["lpts punt excessive-flow-trap"] = "YES"
continue;
if line == "dampening":
data["dampening."] = "YES"
continue;
if line == "non-subscriber-interfaces":
data["non-subscriber-interfaces"] = "YES"
continue;
# Now do the rest
if line.startswith("Hostname"):
splitted = line.split(' ')
data["Hostname"] = splitted[1]
continue;
if line.startswith("loopback"):
splitted = line.split(' ')
data["loopback"] = splitted[1]
continue;
if line.startswith("penalty-rate arp"):
print("ARP")
splitted = line.split(' ')
data["penalty-rate arp"] = splitted[2]
continue;
if line.startswith("penalty-rate icmp"):
splitted = line.split(' ')
data["penalty-rate icmp"] = splitted[2]
continue;
if line.startswith("penalty-rate igmp"):
splitted = line.split(' ')
data["penalty-rate igmp"] = splitted[2]
continue;
if line.startswith("penalty-rate ip"):
splitted = line.split(' ')
data["penalty-rate ip"] = splitted[2]
continue;
if line.startswith("report-threshold"):
splitted = line.split(' ')
data["report-threshold"] = splitted[1]
continue;
return data
if __name__ == "__main__":
inputsDirectory = "inputs"
path = os.path.abspath(inputsDirectory)
fileList = ["{}/{}".format(path,x) for x in os.listdir(inputsDirectory)]
print(fileList)
# Load Each File and Build Dictionary
csvRows = []
for file in fileList:
newRow = parseFile(file)
csvRows.append(newRow)
print(csvRows)
# Output CSV using dictionaries for each file
outputFile = "output.csv"
with open(outputFile, 'w') as csvfile:
fieldnames = ["Hostname",
"loopback",
"lpts punt excessive-flow-trap",
"penalty-rate arp",
"penalty-rate icmp",
"penalty-rate igmp",
"penalty-rate ip",
"dampening",
"non-subscriber-interfaces",
"report-threshold"]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for row in csvRows:
writer.writerow(row)