【发布时间】:2018-08-10 00:40:15
【问题描述】:
使用 PyMySQL python 3.6.3 版本,得到一个DictCursor,然后是fetchall()。我得到了所有数据,.description 说:
(('recdate', 12, None, 19, 19, 0, False), ('outdoorhumidity', 246, None, 9, 9, 3, True), ('outdoortemperature', 246, None, 9, 9, 3, True)).
打印我得到的行,f.ex:
2005-12-31 23:12:00 89.000 -6.667
2005-12-31 23:13:00 89.000 -6.667
2005-12-31 23:15:00 89.000 -6.650
2005-12-31 23:16:00 89.000 -6.650
2005-12-31 23:17:00 89.000 -6.640
注意丢失的一分钟 ...23:14:00 - 但我这样做是为了更大的丢失数据插补项目。所以这是围绕缺失数据的样本数据。通过字典,我想以最好的方式获得不完整的时间序列以及 f ex 3rd 列 - 简单易读的代码?在每种情况下我都必须知道有多少行吗?
import pymysql
dbServerName = "127.0.0.1"
dbUser = "root"
dbPassword = "mypwd"
dbName = "dbname"
charSet = "utf8"
cursorType = pymysql.cursors.DictCursor
connectionObject = pymysql.connect(host=dbServerName, user=dbUser, password=dbPassword,
db=dbName, charset=charSet,cursorclass=cursorType)
try:
cursorObject = connectionObject.cursor()
sqlQuery = "SELECT recdate, outdoorhumidity, outdoortemperature FROM mytable WHERE recdate BETWEEN '2005-12-31 23:12:00' AND '2005-12-31 23:17:00';"
cursorObject.execute(sqlQuery)
#Fetch all the rows - within the cursor? Can this be done?
rows = cursorObject.fetchall()
print(cursorObject.description)
for row in rows:
print(row["recdate"], row["outdoorhumidity"], row["outdoortemperature"])
except Exception as e:
print("Exeception occured:{}".format(e))
finally:
cursorObject.close()
connectionObject.close()
【问题讨论】:
-
请尝试从您的项目中添加一些代码,以便我们了解上下文。此外,其中大部分可以使用内联格式。见how to ask。
-
“f ex 3rd column”是指应该在第三位(索引 2)的缺失列吗?
-
我想在 ML 中将“recdate”和“outdoortemperature”视为“一对”,以便稍后在表格中获得新的插补值
-
“对”是什么意思?你想要两个值的字典,使用
recdate和outdoortemperature作为键? -
spikepaz - 我真的不知道 - 我想要一些东西,尽可能简单地结合我放入一个新的“记录”,只有 recdate -即日期时间和所有其他字段为空,即 NaN,其中 ML 可以在第 3 列中的 NaN(s) 之前和之后看到周围的数据,并计算要插补的数据 - 插入到数据库中。缺失的记录必须用 7 个基础数据的计算值替换,然后是一系列计算字段。 IE。 temp hum press 将成为同一行/记录中的露点。
标签: python-3.x pymysql fetchall