python: open file, feed line to list, process list data

允我心安 提交于 2019-12-24 07:40:22

问题


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)

回答1:


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




回答2:


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)



回答3:


Not entirely sure, bit it looks like you're re-initing the array each time. Can you feed it in one big list?



来源:https://stackoverflow.com/questions/2773416/python-open-file-feed-line-to-list-process-list-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!