python:打开文件,将行输入列表,处理列表数据

发布于 2024-08-31 13:04:38 字数 491 浏览 8 评论 0原文

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

我已经这样做了,但它只处理第一行:

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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

汹涌人海 2024-09-07 13:04:38
graphData = {}

我相信那是一本字典。这就是你的意图吗?

如果您正在寻找列表/数组,可以使用 [] 而不是 {}。之前发帖者说的听起来很正确。每次你都设置 graphData = {} 并因此覆盖过去的任何内容。

array.append(x)

会将一些内容追加到数组中。

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

graphData.append(line).  

Then在循环之后你可以设置
graph_data_dict = {}
graph_data_dict['eth0'] = graph_data_array

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

失去的东西太少 2024-09-07 13:04:38
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)
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)
久光 2024-09-07 13:04:38

不完全确定,看起来您每次都在重新初始化数组。你能把它放在一张大清单里吗?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文