python:打开文件,将行输入列表,处理列表数据
我想处理文件“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信那是一本字典。这就是你的意图吗?
如果您正在寻找列表/数组,可以使用 [] 而不是 {}。之前发帖者说的听起来很正确。每次你都设置 graphData = {} 并因此覆盖过去的任何内容。
会将一些内容追加到数组中。
如果你希望所有行最后都愉快地显示,你可以设置
图表数据 = []
在循环之前。然后每次通过循环执行
Then在循环之后你可以设置
graph_data_dict = {}
graph_data_dict['eth0'] = graph_data_array
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.
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
Then after the loop you can set
graph_data_dict = {}
graph_data_dict['eth0'] = graph_data_array
不完全确定,看起来您每次都在重新初始化数组。你能把它放在一张大清单里吗?
Not entirely sure, bit it looks like you're re-initing the array each time. Can you feed it in one big list?