我有以下 csv 文件,其中包含三个字段 Vulnerability Title、Vulnerability Severity Level、Asset IP Address(显示漏洞名称、漏洞级别和存在该漏洞的 IP 地址)。我正在尝试打印一份报告,该报告将在其旁边的严重性列中列出漏洞以及具有该漏洞的 IP 地址的最后一列列表。
Vulnerability Title Vulnerability Severity Level Asset IP Address
TLS/SSL Server Supports RC4 Cipher Algorithms (CVE-2013-2566) 4 10.103.64.10
TLS/SSL Server Supports RC4 Cipher Algorithms (CVE-2013-2566) 4 10.103.64.10
TLS/SSL Server Supports RC4 Cipher Algorithms (CVE-2013-2566) 4 10.103.65.10
TLS/SSL Server Supports RC4 Cipher Algorithms (CVE-2013-2566) 4 10.103.65.164
TLS/SSL Server Supports RC4 Cipher Algorithms (CVE-2013-2566) 4 10.103.64.10
TLS/SSL Server Supports RC4 Cipher Algorithms (CVE-2013-2566) 4 10.10.30.81
TLS/SSL Server Supports RC4 Cipher Algorithms (CVE-2013-2566) 4 10.10.30.81
TLS/SSL Server Supports RC4 Cipher Algorithms (CVE-2013-2566) 4 10.10.50.82
TLS/SSL Server Supports Weak Cipher Algorithms 6 10.103.65.164
Weak Cryptographic Key 3 10.103.64.10
Unencrypted Telnet Service Available 4 10.10.30.81
Unencrypted Telnet Service Available 4 10.10.50.82
TLS/SSL Server Supports Anonymous Cipher Suites with no Key Authentication 6 10.103.65.164
TLS/SSL Server Supports The Use of Static Key Ciphers 3 10.103.64.10
TLS/SSL Server Supports The Use of Static Key Ciphers 3 10.103.65.10
TLS/SSL Server Supports The Use of Static Key Ciphers 3 10.103.65.100
TLS/SSL Server Supports The Use of Static Key Ciphers 3 10.103.65.164
TLS/SSL Server Supports The Use of Static Key Ciphers 3 10.103.65.164
TLS/SSL Server Supports The Use of Static Key Ciphers 3 10.103.64.10
TLS/SSL Server Supports The Use of Static Key Ciphers 3 10.10.30.81
我想重新创建一个 csv 文件,该文件使用漏洞标题选项卡作为键,并创建第二个名为漏洞严重级别的选项卡,最后一个选项卡将包含所有具有漏洞的 IP 地址
import csv
from pprint import pprint
from collections import defaultdict
import glob
x= glob.glob("/root/*.csv")
d = defaultdict()
n = defaultdict()
for items in x:
with open(items, 'rb') as f:
reader = csv.DictReader(f, delimiter=',')
for row in reader:
a = row["Vulnerability Title"]
b = row["Vulnerability Severity Level"], row["Asset IP Address"]
c = row["Asset IP Address"]
# d = row["Vulnerability Proof"]
d.setdefault(a, []).append(b)
f.close()
pprint(d)
with open('results/ipaddress.csv', 'wb') as csv_file:
writer = csv.writer(csv_file)
for key, value in d.items():
for x,y in value:
n.setdefault(y, []).append(x)
# print x
writer.writerow([key,n])
with open('results/ipaddress2.csv', 'wb') as csv2_file:
writer = csv.writer(csv2_file)
for key, value in d.items():
n.setdefault(value, []).append(key)
writer.writerow([key,n])
因为我不能很好地解释。让我试着简化
假设我有以下 csv
Car model owner
Honda Blue James
Toyota Blue Tom
Chevy Green James
Chevy Green Tom
我正在尝试按以下方式创建此 csv:
Car model owner
Honda Blue James
Toyota Blue Tom
Chevy Green James,Tom
两种解决方案都是正确的。这也是我的最终脚本
import csv
import pandas as pd
df = pd.read_csv('test.csv', names=['Vulnerability Title', 'Vulnerability Severity Level','Asset IP Address'])
#print df
grouped = df.groupby(['Vulnerability Title','Vulnerability Severity Level'])
groups = grouped.groups
#print groups
new_data = [k + (v['Asset IP Address'].tolist(),) for k, v in grouped]
new_df = pd.DataFrame(new_data, columns=['Vulnerability Title' ,'Vulnerability Severity Level', 'Asset IP Address'])
print new_df
new_df.to_csv('final.csv')
谢谢你