我尝试绘制从另一个程序转储到csv的实时(和连续)数据源。我得到了绘图工作但后来意识到我的数据只绘制,直到我执行代码。我相信我的解析功能有问题(这种东西有点新)但我无法弄清楚是什么。
SELECT a.id, a.event_id, a.dt_start, min(b.dt_end)
FROM #starts AS a
LEFT JOIN #ends AS b
ON a.id = b.id
AND a.event_id = b.event_id
AND a.dt_start < b.dt_end
AND DATEDIFF(day, dt_start, dt_end) <= 5
GROUP BY a.id, a.event_id, a.dt_start
来自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()
答案 0 :(得分:1)
代码中有太多未使用的部分,无法真正理解您要实现的目标。这是一个简单的函数:
使用每个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__
。为确保与两者兼容,上面的代码定义了两种方法。