对于分析,有时最好访问select-result的列而不是行。假设我们想要绘制特定记录点(rp)的电压轨迹,并将数据存储在表* 记录 *中,如此
__________________________________
|rp_key | time | value_mV |
----------------------------------
| 1 | 1 | -65 |
----------------------------------
| 1 | 2 | -64.4 |
----------------------------------
| 1 | 3 | -65.3 |
----------------------------------
| 2 | 1 | -67.9 |
----------------------------------
| 2 | 2 | -68.0 |
----------------------------------
现在我想这样画:
import sqlalchemy as sa
import matplotlib.pyplot as plt
# ...
res = session.query(Recording).filter_by(rp_key=1).all()
# HERE: elegant sqlalchemy-way of accessing the columns of the result
plt.plot(time, value_mV)
你会建议什么?
答案 0 :(得分:2)
SQLAlchemy只允许查询指定的列。您所需要的只是transpose resulting matrix。假设Recording
是映射类,代码可能如下所示:
rows = session.query(Recording.time, Recording.value_mV).filter_by(rp_key=1).all()
time, value_mV = zip(*rows)
plt.plot(time, value_mV)