Skip to main content
 首页 » 编程设计

python之在 python 中解析数据提要

2025年05月04日17TianFang

我正在尝试绘制从另一个程序转储到 csv 中的实时(连续)数据馈送。我让绘图工作,但后来意识到我的数据只在我执行代码时才绘制。我相信我的解析函数有问题(这方面有点新),但我不知道是什么。

import threading 
import csv  
import dateutil.parser  
import datetime 
import time 
import matplotlib 
import matplotlib.pyplot as plt 
from matplotlib import animation 
 
log = "sample.csv" 
data = "" 
mytime = "" 
thickness = "" 
times = ""    
 
#attempt at continuously reading my data source  
def parser(): 
    with open(log, 'r') as f: 
        global data 
        reader = csv.reader(f, delimiter=',')     
        data = reader.readlines() 
        time_thickness_data = [] 
        while 1: 
            last_pos = f.tell() 
            next_line = f.readline() 
            if not next_line: 
                time.sleep(1) 
                f.seek(last_pos) 
            else: 
                mytime, thickness = splitter(next_line) 
                time_thickness_data.append([mytime, thickness]) 
 
def splitter(line): 
    mytime = line[0] 
    thickness = line[3] 
    return (mytime, thickness) 
    times = [] 
    for date in mytime:  
        _ = dateutil.parser.parse(date)  
        times.append(datetime.datetime.strftime(_,'%H'))  
 
def main(): 
    a = threading.Thread(target=parser) 
    b = threading.Thread(target=splitter) 
    a.start() 
    b.start() 
 
if __name__ == "__main__": 
    main() 
 
#goes on to animated plot using times for x axis and thickness for y 
fig = plt.figure() 
axes = fig.add_subplot(111) 
line, = axes.plot([], [], '.') 
plt.show(block=False) #i had to use this to get my plot to show up 
 
def init(): 
    line.set_data([],[])  
    return line, 
 
def animate(i): 
    a = 50 * i 
    xdata = times[:a] 
    ydata = thickness[:a] 
    line.set_data(xdata, ydata) 
    plt.draw() 
    axes.relim() 
    axes.autoscale_view(True,True,True) 
    return line, 
 
anim = animation.FuncAnimation(fig, animate, init_func=init,   
                           interval=1, blit=True)                            
plt.show() 

来自 csv 的示例行如下所示: 2015-07-25 14:54:50.786774,1,0,439.85,,,,0,0,

请您参考如下方法:

您的代码中有太多未使用的部分,无法真正理解您试图实现的目标。这是一个简单的函数:

  • 打开一个 csv 文件
  • 反复从中读取行,等待到达文件末尾时添加新行
  • 使用每个 csv 行的解码列表调用回调函数

    def csv_process(filename, callback, delay=1): 
        class waiter:     # wrapper around a file object to wait for new lines on EOF 
            def __init__(self, fd, delay=1): 
                self.fd = fd 
                self.delay = delay 
            def __iter__(self): 
                return self 
            def next(self):  # try to read a line of wait delay seconds 
                while True: 
                    line = fd.readline() 
                    if line: 
                        return line 
                    time.sleep(self.delay) 
            def __next__(self):  # ensure compatibility with Python3.x 
                return self.next() 
        with open(filename, "rb") as fd: 
            rows = csv.reader(waiter(fd, delay), delimiter=',') 
            for row in rows: 
                callback(row) 
    

Python2 迭代器应实现next,而Python3 迭代器应实现__next__。为了确保两者的兼容性,上面的代码定义了这两种方法。