【发布时间】:2018-02-28 23:36:23
【问题描述】:
我有一个目录,其中包含从 pcap 转换为 csv 的非常大的 csv 文件。
我正在尝试遍历该目录中的每个 csv 文件并获取最常见的源 IP 地址(第 2 列)。
目前我的输出不正确,因为似乎我已经设法让每个文件在开始之前将其值转储到下一个文件中。每个文件似乎都有相同的 ip,我知道情况并非如此。
ipCounter = collections.Counter()
#iterate through all of the files in the directory, using glob
for filename in glob.glob('/path/to/directory/*'):
with open(filename) as input_file:
#skip column titles
input_file.next()
for row in csv.reader(input_file, delimiter=','):
ipCounter[row[2]] += 1
print 'Source IPs most common in: %s' % filename
print ipCounter.most_common()
我并不完全是 Python 专家,所以可能有更好的方法来做到这一点,但这是我目前所得到的。
【问题讨论】:
标签: python python-2.7 csv ip