蟒蛇:打开文件,供稿线列表,处理列表数据 [英] python: open file, feed line to list, process list data

查看:92
本文介绍了蟒蛇:打开文件,供稿线列表,处理列表数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想处理文件 output.log中的数据并将其提供给graphdata ['eth0]

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?

如果您要查找列表/数组,可以使用[]代替{}。以前的海报所说的听起来很正确。每次通过设置graphData = {},因此都会覆盖过去的任何内容。

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.

如果您希望所有行最后都愉快地显示,您可以在循环前设置
graphData = []
。然后每次通过循环执行

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).  

然后在循环之后,您可以设置
graph_data_dict = {}
graph_data_dict [ 'eth0'] = graph_data_array

Then after the loop you can set graph_data_dict = {} graph_data_dict['eth0'] = graph_data_array

这篇关于蟒蛇:打开文件,供稿线列表,处理列表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆