[英]How can you convert datetime.date from mysql into unix timestamp
Have spent a few hours trying to figure this out unsuccessfully. 花了几个小时试图解决这个问题。
I've imported some 'date' (not datetime) fields from MySQL into a python list. 我已经从MySQL导入了一些“日期”(而非日期时间)字段到python列表中。 If I print the list, the values will show up as: 如果我打印列表,则这些值将显示为:
my_list = [[100, datetime.date(2013, 3, 11)], [101, datetime.date(2013, 4, 13,)],[102,datetime.date(2013, 4, 13)]
(...where 101, 102, 103 are my corresponding event_id's, you can disregard them) (...其中101、102、103是我对应的event_id,您可以忽略它们)
I've found that the following will get the conversion correct: 我发现以下内容将使转换正确:
unix_time = time.mktime(datetime.date(2009,2,17).timetuple())
However, if I try to do something like: 但是,如果我尝试执行以下操作:
for x in my_list:
x[1] = time.mktime(datetime.date(x[1]).timetuple())
It will not work. 不起作用。
First solution was ugly, new edit: 第一个解决方案是丑陋的,新编辑:
for x in my_list:
x[1] = time.mktime(x[1].timetuple())
should get you your list. 应该会得到您的清单。 No need to wrap the datetime.date in another datetime.date 无需将datetime.date包装在另一个datetime.date中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.