gpt4 book ai didi

python - 使用 Python2 合并 CSV 行并保留单个任意列中的数据

转载 作者:太空宇宙 更新时间:2023-11-03 15:32:32 25 4
gpt4 key购买 nike

我知道关于这个主题有很多问题,但答案并没有得到很好的解释,因此很难适应我的用例。一个here看起来很有前途,但语法相当复杂,我很难理解和适应它。

我需要将 Nessus 的原始 CSV 输出转换为标准格式,这实际上会转储许多列,仅保留每个发现的严重性、IP 地址和输出。我已经将一个脚本放在一起,如果发现的结果位于多个主机/端口上,则每个主机/端口都有不同的行。

我需要的是根据漏洞名称合并行,但只保留 IP 地址数据。

示例输入 - 为方便起见而缩短

High,10.10.10.10,MS12-345(this is the name),Hackers can do bad things
High,10.10.10.11,MS12-345(this is the name),Hackers can do bad things

示例输出

High,10.10.10.10 10.10.10.11,MS12-345(this is the name),Hackers can do bad things

下面是到目前为止我的脚本。如果您使您的答案能够轻松适应(阅读:防白痴)以供 future 的读者使用,我将不胜感激,我相信他们也会的。

奖金:

有时,具有相同名称的结果的输出字段会有所不同,有时则相同。如果您有时间,为什么不帮助一个人检查这一点,并在输出中存在差异时以与 IP 地址相同的方式进行附加?

import sys
import csv

def manipulate(inFile):

with open(inFile, 'rb') as csvFile:
fileReader = csv.reader(csvFile, dialect='excel')

# Check for multiple instances of findings and merge the rows
# This happens when the finding is on multiple hosts/ports

//YOUR CODE WILL GO HERE (Probably...)

# Place findings into lists: crits, highs, meds, lows for sorting later
crits = []
highs = []
meds = []
lows = []

for row in fileReader:

if row[3] == "Critical":
crits.append(row)
elif row[3] == "High":
highs.append(row)
elif row[3] == "Medium":
meds.append(row)
elif row[3] == "Low":
lows.append(row)

# Open an output file for writing
with open('output.csv', 'wb') as outFile:
fileWriter = csv.writer(outFile)

# Add in findings from lists in order of severity. Only relevant columns included
for c in crits:
fileWriter.writerow( (c[3], c[4], c[7], c[12]) )

for h in highs:
fileWriter.writerow( (h[3], h[4], h[7], h[12]) )

for m in meds:
fileWriter.writerow( (m[3], m[4], m[7], m[12]) )

for l in lows:
fileWriter.writerow( (l[3], l[4], l[7], l[12]) )


# Input validation
if len(sys.argv) != 2:
print 'You must provide a csv file to process'
raw_input('Example: python nesscsv.py foo.csv')
else:
print "Working..."
# Store filename for use in manipulate function
inFile = str(sys.argv[1])
# Call manipulate function passing csv
manipulate(inFile)

print "Done!"
raw_input("Output in output.csv. Hit return to finish.")

最佳答案

以下解决方案使用 OrderedDict 以保留行顺序的方式收集行,同时还允许按漏洞名称查找任何行。

import sys
import csv
from collections import OrderedDict

def manipulate(inFile):

with open(inFile, 'rb') as csvFile:
fileReader = csv.reader(csvFile, dialect='excel')

# Check for multiple instances of findings and merge the rows
# This happens when the finding is on multiple hosts/ports

# Dictionary mapping vulns to merged rows.
# It's ordered to preserve the order of rows.
mergedRows = OrderedDict()

for newRow in fileReader:
vuln = newRow[7]
if vuln not in mergedRows:
# Convert the host and output fields into lists so we can easily
# append values from rows that get merged with this one.
newRow[4] = [newRow[4], ]
newRow[12] = [newRow[12], ]
# Add row for new vuln to dict.
mergedRows[vuln] = newRow
else:
# Look up existing row for merging.
mergedRow = mergedRows[vuln]
# Append values of host and output fields, if they're new.
if newRow[4] not in mergedRow[4]:
mergedRow[4].append(newRow[4])
if newRow[12] not in mergedRow[12]:
mergedRow[12].append(newRow[12])

# Flatten the lists of host and output field values into strings.
for row in mergedRows.values():
row[4] = ' '.join(row[4])
row[12] = ' // '.join(row[12])

# Place findings into lists: crits, highs, meds, lows for sorting later
crits = []
highs = []
meds = []
lows = []

for row in mergedRows.values():

if row[3] == "Critical":
crits.append(row)
elif row[3] == "High":
highs.append(row)
elif row[3] == "Medium":
meds.append(row)
elif row[3] == "Low":
lows.append(row)

# Open an output file for writing
with open('output.csv', 'wb') as outFile:
fileWriter = csv.writer(outFile)

# Add in findings from lists in order of severity. Only relevant columns included
for c in crits:
fileWriter.writerow( (c[3], c[4], c[7], c[12]) )

for h in highs:
fileWriter.writerow( (h[3], h[4], h[7], h[12]) )

for m in meds:
fileWriter.writerow( (m[3], m[4], m[7], m[12]) )

for l in lows:
fileWriter.writerow( (l[3], l[4], l[7], l[12]) )


# Input validation
if len(sys.argv) != 2:
print 'You must provide a csv file to process'
raw_input('Example: python nesscsv.py foo.csv')
else:
print "Working..."
# Store filename for use in manipulate function
inFile = str(sys.argv[1])
# Call manipulate function passing csv
manipulate(inFile)

print("Done!")
raw_input("Output in output.csv. Hit return to finish.")

关于python - 使用 Python2 合并 CSV 行并保留单个任意列中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42806438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com