【发布时间】:2016-12-30 17:28:46
【问题描述】:
我有一个读取 CSV 文件的脚本 csv file
Asset IP Address,Vulnerability Title
50.103.128.11,Partition Mounting Weakness
10.103.128.11,UDP IP ID Zero
10.103.128.11,Root's umask value is unsafe
0.103.128.11,Root's umask value is unsafe
20.103.128.11,Root's umask value is unsafe
10.103.128.11,ICMP timestamp response
22.103.128.11,ICMP timestamp response
10.103.128.11,UDP IP ID Zero
10.103.129.11,Partition Mounting Weakness
在运行我的脚本之后
import csv
from pprint import pprint
#with open('test.csv', 'rb') as f:
# reader = csv.DictReader(f, delimiter=',')
# for row in reader:
# print row
#dict = {a:[], b:[]}
dict = {}
with open('test.csv', 'rb') as f:
reader = csv.DictReader(f, delimiter=',')
for row in reader:
a = row["Vulnerability Title"]
b = [row["Asset IP Address"]]
#b = row(["Asset IP Address"])
#dict = {a:[], b:[]}
if a in dict:
#print row["Vulnerability Title"]
#if row["Vulnerability Title"] in dict:
dict[a].append(b)
else:
dict[a] = b
pprint(dict)
读取漏洞列表并使用具有该漏洞的 ips 创建一个字典。但是我的结果是一个带有一个额外括号的列表。想伸出手,看看有人有更好的想法或可以帮助我。 results
{'ICMP timestamp response': ['10.103.128.11', ['22.103.128.11']],
'Partition Mounting Weakness': ['50.103.128.11', ['10.103.129.11']],
"Root's umask value is unsafe": ['10.103.128.11',
['0.103.128.11'],
['20.103.128.11']],
'UDP IP ID Zero': ['10.103.128.11', ['10.103.128.11']]}
【问题讨论】:
-
你不需要在
b = [row["Asset IP Address"]]中的外部[]
标签: python list csv dictionary append