【问题标题】:Python SQL: Error Decimal('1367') is not JSON serializablePython SQL:错误十进制('1367')不是 JSON 可序列化的
【发布时间】:2020-12-07 11:23:53
【问题描述】:

我在 Python 中有一条 SQL 语句,我试图从我的数据库中获取最常见的错误。

def alarms(session, machine_id, ts, ts_start, machine_serial):
    stmt_raw = '''
        WITH count_data AS (
        SELECT
            identifier.name,
            PDA_BitCount.ts,
            PDA_BitCount.high,
            alarm_info.ala_text
        FROM
            machine
        INNER JOIN
            identifier
            ON
                identifier.machine_id = :machineid_arg
        INNER JOIN
            PDA_BitCount
            ON
                PDA_BitCount.identifier_id = identifier.id
        LEFT JOIN
            alarm_info
            ON
                alarm_info.ala_group = CAST(SUBSTR(identifier.name, 8, 2) AS INTEGER) AND
                alarm_info.ala_number = CAST(SUBSTR(identifier.name, 11, 2) AS INTEGER) AND
                alarm_info.machine_id = :machineid_arg
        WHERE
            machine.serial = :machine_serial_arg AND
            identifier.name LIKE 'Alarm_G%' AND
            PDA_BitCount.ts > :ts_start_arg AND 
            PDA_BitCount.ts < :ts_arg AND
            alarm_info.ala_language = 'de'
        ), commulated_data AS (
        SELECT
            count_data.name AS alias,
            COUNT(*) AS count,
            REPLACE(ala_text, '{TextSnippet_6}', '') AS ala_text
        FROM
            count_data
        WHERE
            count_data.high = TRUE
        GROUP BY
            count_data.name
        ORDER BY
            count DESC
        ), CalcSum AS (
        SELECT SUM(commulated_data.count) AS sum
        FROM commulated_data
        ) SELECT *
        FROM commulated_data
        JOIN CalcSum
        WHERE commulated_data.count != 0
        LIMIT 3
    '''
    stmt_args = {
        'machineid_arg': machine_id,
        'ts_arg': ts,
        'ts_start_arg': ts_start,
        'machine_serial_arg': machine_serial,
        'includes_arg': [''],
        'excludes_arg': [
            'G01N01', 'G01N02', 'G01N03', 'G01N04', 'G01N05', 'G01N06', 'G01N07', 'G01N08', 'G01N09',
            'G01N10', 'G01N11', 'G01N12', 'G01N13', 'G01N14', 'G01N15', 'G01N16', 'G01N17', 'G01N18',
            'G01N19', 'G01N20', 'G01N21', 'G01N22', 'G01N23', 'G01N24', 'G01N25', 'G01N26', 'G01N27',
            'G01N28', 'G01N29', 'G01N30', 'G01N31', 'G01N32', 'G01N33', 'G01N34', 'G01N35', 'G01N36',
            'G01N37', 'G01N38', 'G01N39', 'G01N40', 'G01N41', 'G01N42', 'G01N43', 'G01N44', 'G01N45',
            'G01N46', 'G01N47', 'G01N48', 'G01N49', 'G01N50', 'G01N51', 'G01N52', 'G01N53', 'G01N54',
            'G01N55', 'G01N56', 'G01N57', 'G01N58', 'G01N59', 'G01N60', 'G01N61', 'G01N62', 'G01N63',
            'G01N64', 'G24N01', 'G24N02', 'G24N03', 'G24N04', 'G24N05', 'G24N06', 'G24N07'
        ],
    }

    stmt = text(stmt_raw).columns(
        # ts_insert = ISODateTime
    )

    result = session.execute(stmt, stmt_args)

    alarms = []

    for row in result:
        alarms.append({
            'alias': row[0],
            'count': row[1],
            'ala_text': row[2],
            'sum': row[3],
        })

    return alarms

问题在于我上次加入 CalcSum 然后我收到此错误,我不知道如何处理它。找到了足够多的主题,但不是在这种情况下。我该如何处理?没有这个加入我的结果就好了,但我需要来自CalcSum 的这个sum

【问题讨论】:

    标签: python sql


    【解决方案1】:

    问题是psycopg2 更喜欢返回更稳定的Decimal 类型而不是python 标准float。这对防止浮点错误非常有用,但它留下了让用户可序列化 JSON 的责任。

    如果我正确理解您的查询,countsum 实际上都是整数。如果这是真的,只需将它们投射到int。否则,将它们投射到float

    alarms = []
    
    for row in result:
        alarms.append({
            'alias': row[0],
            'count': int(row[1]),
            'ala_text': row[2],
            'sum': int(row[3]),
        })
    

    如果我可以对您的代码提出一些建议:

    最好使用列表解构来给row[0] 一个更直观的名称:

    alarms = []
    
    for alias, count, ala_text, sum in result:
        alarms.append({
            'alias': alias,
            'count': int(count),
            'ala_text': ala_text,
            'sum': int(sum),
        })
    

    您可以使用列表推导一次性构建列表:

    alarms = [{
        'alias': alias,
        'count': int(count),
        'ala_text': ala_text,
        'sum': int(sum),
    } for alias, count, ala_text, sum in result]
    

    【讨论】:

      猜你喜欢
      • 2023-03-23
      • 2023-03-15
      • 1970-01-01
      • 2015-04-24
      • 1970-01-01
      • 2018-09-08
      • 2022-09-28
      • 1970-01-01
      • 2017-04-22
      相关资源
      最近更新 更多