Python/mysql:结果分为两个列表

1 投票
1 回答
675 浏览
提问于 2025-04-16 23:18

我在用Python脚本和matplotlib来绘制MySQL的查询结果。

import MySQLdb
import matplotlib.pyplot as plt

   conn = MySQLdb.connect (host = "localhost",
                           user = "testuser",
                           passwd = "testpass",
                           db = "test")
   cursor = conn.cursor ()
   cursor.execute ("SELECT id,secondid from table limit 10")
   row = cursor.fetchall()
   print "RESULT:", row[0]
   cursor.close ()
   conn.close ()

我打算通过简单地传递这两个列表来绘图,像这样:

id,ts=zip(*row)  #cThis will solve the problem


plt.plot(id,secondid)
plt.grid()

但是我有点困惑,因为查询结果是一个元组的列表,所以我不知道怎么创建这两个列表。请给点建议。

1 个回答

1

答案是

id,secondid=zip(*row)

撰写回答