1

我在这样的文本文件中有一堆数据:

99150, 2012-05-18 14:30:08.592276
100350, 2012-05-18 14:31:09.093357
97710, 2012-05-18 14:32:09.583485
94980, 2012-05-18 14:33:10.047794
95670, 2012-05-18 14:34:10.559798
97170, 2012-05-18 14:35:11.073576
98850, 2012-05-18 14:36:11.562930
98280, 2012-05-18 14:37:12.058591
97950, 2012-05-18 14:38:12.547585
102510, 2012-05-18 14:39:13.053431

我想对其进行简单的绘图并输出图像。我从以下开始:

#!/bin/python

import csv
import matplotlib.pyplot as plt
import numpy as np

filename="pps_counter.log"

def getColumn(filename, column):
    results = csv.reader(open(filename), delimiter=",")
    return [result[column] for result in results]

time = getColumn(filename,1)
packets = getColumn(filename,0)

plt.figure("Packets Per Minute")
plt.xlabel("Time(minutes)")
plt.ylabel("Number of Packets")
plt.plot(time,packets)

当我运行它时,我收到以下错误:

Traceback (most recent call last):
  File "plotter.py", line 16, in <module>
    time = getColumn(filename,1)
  File "plotter.py", line 14, in getColumn
    return [result[column] for result in results]
IndexError: list index out of range

任何人都可以帮忙吗?

4

2 回答 2

1

我建议使用csv2rec(或带有转换器功能的 genfromtxt )。这会将时间转换为 python 日期时间,您可以使用matplotlib.

from matplotlib.mlab import csv2rec
import matplotlib.pyplot as plt 

data = csv2rec('pps_counter.log', names=['packets', 'time'])

plt.plot_date(data['time'], data['packets'])
plt.xlabel("Time(minutes)")
plt.ylabel("Number of Packets")
plt.title("Packets Per Minute")

plt.show()
于 2012-05-18T21:44:28.070 回答
0

我会使用一些错误检查,例如

return [ result[column] if column < len( result ) else None for result in results]

看起来您的文件中有一行缺少,

于 2012-05-18T19:06:58.373 回答