问题描述
问题:
只要我得到以下查询的结果:
Whenever I get the result of the following query:
"SELECT email, url, `timestamp` FROM `transaction` WHERE `email` != '' AND `timestamp` >= \'" + dateonlystring + "\' AND `timestamp` < DATE(DATE_ADD(\'" + dateonlystring + "\', INTERVAL 1 DAY))"
出现:
('[email protected]','',datetime.datetime(2015,2,25,10,11,19))
('[email protected]', 'http://sample.url.com', datetime.datetime(2015, 2, 25, 10, 11, 19))
为什么它是datetime.datetime(2015,2,25,10,11,19))?
Why is it a datetime.datetime(2015, 2, 25, 10, 11, 19))?
我怎么能把它变成这个:
And how can I turn that into this:
[email protected]~ 10:11:19
[email protected]~http://sample.url.com~2015-02-25 10:11:19
我的代码/我尝试过的内容:
#Date
#datestamp = datetime.strptime('2015-02-25 00:00:00', "%Y-%m-%d %H:%M:%S")
datestamp = datetime.now()
dateonlystring = str(datestamp.date().strftime("%Y-%m-%d %H:%M:%S"))
datetimestring = str(datestamp.now().strftime("%Y%m%d%H%M%S"))
#Dunchangeme:
#Query:
q_getstuff = "SELECT email, url, `timestamp` FROM `transaction` WHERE `email` != '' AND `timestamp` >= \'" + dateonlystring + "\' AND `timestamp` < DATE(DATE_ADD(\'" + dateonlystring + "\', INTERVAL 1 DAY))"
try:
con = mdb.connect(hostname, username, password, database)
cur = con.cursor()
cur.execute(q_getstuff)
result = cur.fetchall()
for row in result:
tuplecrap = str(row).replace('(', '').replace(')', '').replace('\'', '').replace(', ', '~')
print tuplecrap
请注意,注释的第一个 datestamp $如果用户打算去某个特定日期(而不是现在),则在那里c $ c>。
Of note is that the commented first datestamp
is there if the user intends to go to a specific date (as opposed to only now).
最简单的方法是简单地摆脱括号和逗号,就像我上面所做的那样,但是如果不使用复杂的正则表达式解决方案就无法解决问题,而且我敢肯定这不是正确的方法。
The easiest way would be to simply get rid of the parenthesis and commas, as I did above, but that doesn't solve the problem without going into complex regex solutions, and I'm pretty sure it's not the right way.
有任何建议吗?
推荐答案
您在每个结果行中都有包含3个项目的元组,只需将其拆包即可:
You have tuples with 3 items in each result row, just unpack it:
for row in result:
email, url, date = row
# here you can format date as you want
这篇关于python tuple返回datetime.datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!