Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python AWS Lmbda TypeError:datetime.datetime(2012,8,8,21,46,24,862000)不可JSON序列化_Python_Amazon Web Services_Aws Lambda - Fatal编程技术网

Python AWS Lmbda TypeError:datetime.datetime(2012,8,8,21,46,24,862000)不可JSON序列化

Python AWS Lmbda TypeError:datetime.datetime(2012,8,8,21,46,24,862000)不可JSON序列化,python,amazon-web-services,aws-lambda,Python,Amazon Web Services,Aws Lambda,我正试图从m RDS数据库中返回一套装备。dte字段均采用DATETIME格式,这导致我出现以下错误: TypeError: datetime.datetime(2012, 8, 8, 21, 46, 24, 862000) is not JSON serializable 我怎样才能避开这件事 这是我的密码: import pymysql import json from rds import * #rds settings host = rds_host name = rds_name

我正试图从m RDS数据库中返回一套装备。dte字段均采用DATETIME格式,这导致我出现以下错误:

TypeError: datetime.datetime(2012, 8, 8, 21, 46, 24, 862000) is not JSON serializable
我怎样才能避开这件事

这是我的密码:

import pymysql
import json
from rds import *

#rds settings
host  = rds_host
name = rds_name
password = rds_password
db_name = rds_db_name
port = rds_port

try:
    conn = pymysql.connect(host = host, user=name, passwd=password, db=db_name, connect_timeout=5)

except:
    raise Exception('Database Error: The server encountered an unexpected condition which prevented it from fulfilling the request.')


def handler(event, context):

    cur = conn.cursor(pymysql.cursors.DictCursor)
    #selects a user from the database
    query = "SELECT * FROM Outfits WHERE outfit_id = '" + event['outfitId'] + "' LIMIT 1"
    #runs the SQL query
    try:
        cur.execute(query)
    except:
        raise Exception('Internal Error: The server encountered an unexpected condition which prevented it from fulfilling the request.')

    #stores the downloaded record into the user variable
    rows = cur.fetchone()
    return result

尚未使用AWS Lambda,但此问题是特定于python的
datetime.datetime
对象不能被JSON序列化,您需要在JOSN序列化之前将它们转换为字符串。您可以使用将
datetime.datetime
对象转换为字符串。

您不能以其他格式存储datetime吗?看起来查询数据库的响应是JSON格式的,将日期存储为字符串肯定会更有效率?

但是我需要查询数据库,获得按日期排序的结果,因此需要datetime格式来执行此操作。我不认为这与上面给出的问题重复。OP无法控制AWS如何将MySQL查询转换为Json,因为AWS本身是在Lambda函数之外完成的。因此,需要对查询结果本身进行修复,而不是修改JSON编码器(因为不知道AWS如何将结果转换为JSON字符串)。