Tail 读取不断增长的动态文件并提取两列,然后打印图表

发布于 2024-08-10 23:24:21 字数 81 浏览 4 评论 0原文

读取 1 GB 文件(其中记录了时间序列数据)并生成包含两列(一个是时间,另一个是数字)的实时图表的最佳方法是什么?我发现您有不同的方式来调整文件。

What is the best way to read a 1 GB file that gets time series data logged in it and generate a real time graph with two of its columns (one time and other a number)? I see that you have different ways of tailign the file.

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

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

发布评论

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

评论(3

糖粟与秋泊 2024-08-17 23:24:21

听起来对于 RRDTool 来说是一份不错的工作。

但如果你想坚持使用 Python,我会使用 tail 将数据流式传输到我的程序中(这是假设文件被连续写入,否则 Python 中的直接 open() 就可以了)。

tail -F data.log | python myprogram.py

myprogram.py 可能类似于:

import sys

p = ... # create a pylab plot instance 
for line in sys.stdin:
    elements = line.split(',') # or whatever separator your file has in it
    p.add(element[0], element[1]) # add data to the pylab plot instance

Sounds like a good job for RRDTool.

But if you want to stick with Python, I would use tail to stream the data into my program (this is assuming the file is continuously written to, otherwise a straight open() in Python will work).

tail -F data.log | python myprogram.py

myprogram.py could look something like:

import sys

p = ... # create a pylab plot instance 
for line in sys.stdin:
    elements = line.split(',') # or whatever separator your file has in it
    p.add(element[0], element[1]) # add data to the pylab plot instance
瑾兮 2024-08-17 23:24:21

这是 unix 管道,它有 3 个部分:tail'er、过滤器 (gawk) 和绘图仪 (python)。

tail -f yourfile.log | gawk '/PCM1/{print $21; fflush();}' | python -u tailplot.py

这是 python 脚本。您可以向其提供 1 (y) 或 2 (xy) 列数据。如果您不使用 gawk,请务必弄清楚如何禁用缓冲。例如sed -u

pa-poca$ cat ~/tailplot.py

import math
import time
import sys
import pylab

pylab.ion()
pylab.xlabel("X")
pylab.ylabel("Y")

x = []
y = []
counter = 1
while True :
    line = sys.stdin.readline()
    a = line.split()
    if len(a) == 2:
      x.append(a[0])
      y.append(a[1])
    elif len(a) == 1:
      x.append(counter)
      y.append(a[0])
      counter = counter + 1
    pylab.plot(x, y, 'b')
    pylab.draw()

Here's the unix pipe which has 3 parts: the tail'er, the filter (gawk), and the plotter (python).

tail -f yourfile.log | gawk '/PCM1/{print $21; fflush();}' | python -u tailplot.py

and here is the python script. You can feed it 1 (y) or 2 (x y) columns of data. If you don't use gawk, be sure to figure out how to disable buffering. sed -u for example.

pa-poca$ cat ~/tailplot.py

import math
import time
import sys
import pylab

pylab.ion()
pylab.xlabel("X")
pylab.ylabel("Y")

x = []
y = []
counter = 1
while True :
    line = sys.stdin.readline()
    a = line.split()
    if len(a) == 2:
      x.append(a[0])
      y.append(a[1])
    elif len(a) == 1:
      x.append(counter)
      y.append(a[0])
      counter = counter + 1
    pylab.plot(x, y, 'b')
    pylab.draw()
明媚殇 2024-08-17 23:24:21

正如约翰提到的,您可以将尾部输出输入到您的文件中,但是如果您由于某种原因想要处理文件中的所有内容,并且还想要一个有点动态图的示例,这里是

import math
import time
import pylab  

def getDataTest(filePath):
    s = 0
    inc = .05
    x_list=pylab.arange(0, 5.0, 0.01)
    while 1:
        s += inc
        if abs(s) > 1:
            inc=-inc

        y_list = []
        for x in x_list:
            x += s
            y = math.cos(2*math.pi*x) * math.exp(-x)
            y_list.append(y)

        yield x_list, y_list

def tailGen(filePath):
    f = open(filePath)
    #f.seek(0, 2) # go to end
    for line in f: yield line
    while 1:
        where = f.tell()
        line = f.readline()
        if line:
            yield line
        else:
            time.sleep(.1)
            f.seek(where)

def getData(filePath):
    x_list = []
    y_list = []
    maxCount = 10
    for line in tailGen(filePath):
        # get required columns
        tokens = line.split(",")
        if len(tokens) != 2:
            continue
        x, y = tokens
        x_list.append(x)
        y_list.append(y)
        if len(x_list) > maxCount:
            x_list = x_list[-maxCount:]
            y_list = x_list[-maxCount:]
            yield x_list, y_list

pylab.ion()
pylab.xlabel("X")
pylab.ylabel("Y")

dataGen = getData("plot.txt") # getDataTest("plot.txt") #
x_list, y_list = dataGen.next()
plotData, = pylab.plot(x_list, y_list, 'b')
#pylab.show()
pylab.draw()
for (x_list, y_list) in dataGen:
    time.sleep(.1)
    plotData, = pylab.plot(x_list, y_list, 'b')
    pylab.draw()

您可以从中选取元素,我认为它会解决你的问题。

As John mentioned, you can input the tail output into your file, but if you due to some reason wants to handle everything in your file and also want an example of somewhat dynamic graph, here it is

import math
import time
import pylab  

def getDataTest(filePath):
    s = 0
    inc = .05
    x_list=pylab.arange(0, 5.0, 0.01)
    while 1:
        s += inc
        if abs(s) > 1:
            inc=-inc

        y_list = []
        for x in x_list:
            x += s
            y = math.cos(2*math.pi*x) * math.exp(-x)
            y_list.append(y)

        yield x_list, y_list

def tailGen(filePath):
    f = open(filePath)
    #f.seek(0, 2) # go to end
    for line in f: yield line
    while 1:
        where = f.tell()
        line = f.readline()
        if line:
            yield line
        else:
            time.sleep(.1)
            f.seek(where)

def getData(filePath):
    x_list = []
    y_list = []
    maxCount = 10
    for line in tailGen(filePath):
        # get required columns
        tokens = line.split(",")
        if len(tokens) != 2:
            continue
        x, y = tokens
        x_list.append(x)
        y_list.append(y)
        if len(x_list) > maxCount:
            x_list = x_list[-maxCount:]
            y_list = x_list[-maxCount:]
            yield x_list, y_list

pylab.ion()
pylab.xlabel("X")
pylab.ylabel("Y")

dataGen = getData("plot.txt") # getDataTest("plot.txt") #
x_list, y_list = dataGen.next()
plotData, = pylab.plot(x_list, y_list, 'b')
#pylab.show()
pylab.draw()
for (x_list, y_list) in dataGen:
    time.sleep(.1)
    plotData, = pylab.plot(x_list, y_list, 'b')
    pylab.draw()

You can pickup elements from it and I think it will solve your problem.

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