python: open file, feed line to list, process list data
I want to process the data in the file "output.log" and feed it to graphdata['eth0]
I have done this but it process only the first line:
开发者_运维知识库logread = open("output.log", "r").readlines()
for line in logread:
print "line", line
i = line.rstrip("\n")
b = float(i)
colors = [ (0.2, 03, .65), (0.5, 0.7, .1), (.35, .2, .45), ]
graphData = {}
graphData['eth0'] = [b]
cairoplot.dot_line_plot("./blog", graphData, 500, 500, axis=True, grid=True, dots=True, series_colors=colors)
graphData = {}
I believe that is a dictionary. Is that what you intended?
If you're looking for a list/array you can use [] instead of {}. What a previous poster said sounds correct. Every time through you are setting graphData = {} and therefore overwriting anything from the past.
array.append(x)
will append something to an array.
If you want all lines displayed all happily at the end you could set graphData = [] before the loop. Then each time through the loop do the
graphData.append(line).
Then after the loop you can set graph_data_dict = {} graph_data_dict['eth0'] = graph_data_array
logread = open("output.log", "r").readlines()
for line in logread:
print "line", line
i = line.rstrip("\n")
b = float(i)
colors = [ (0.2, 03, .65), (0.5, 0.7, .1), (.35, .2, .45), ]
graphData = {}
graphData['eth0'] = [b]
cairoplot.dot_line_plot("./blog", graphData, 500, 500, axis=True, grid=True, dots=True, series_colors=colors)
Not entirely sure, bit it looks like you're re-initing the array each time. Can you feed it in one big list?
精彩评论