ppjer 发表于 2014-10-21 12:35

webpy返回时间问题?

库里有个字段格式如:time2014-09-15 14:56:33 ;
这时间字段不能返回,报错如下:
datetime.datetime(2014, 10, 19, 1, 0) is not JSON serializable

出错代码如下:
cursor.execute('select student,time from school)
lis = []
for r in cursor.fetchall():
      lis.append(r)
return json.dumps(lis,encoding='utf-8')


现在想返时间格式为: 2014-09-15 该怎么返回??

qinyiwang 发表于 2014-10-21 13:08

这里有个代码可以参考下(Python3.2),不过用的时间是当前时间:

from datetime import *
#获得当前时间
ctime = datetime.now()
#转换为指定的格式:
otherStyleTime = ctime.strftime("%Y-%m-%d")
print(otherStyleTime)

qinyiwang 发表于 2014-10-21 13:10

本来datetime.now() 获得的时间格式也是类似于2014-09-15 14:56:33

ppjer 发表于 2014-10-21 14:36

回复 2# qinyiwang

你这种方式不行啊,因为select 还有其它字段,比如student; cursor.execute('select student,time from school)
在我提供的代码上该怎么处理?

qinyiwang 发表于 2014-10-21 15:49

回复 4# ppjer
那你能分别select吗,再把数据综合起来?

   

ppjer 发表于 2014-10-22 10:05

解决了,转成字符串即可。
页: [1]
查看完整版本: webpy返回时间问题?