Python-导致DB INSERT上出现keyerror的NULL数据

Python-导致DB INSERT上出现keyerror的NULL数据,python,json,postgresql,Python,Json,Postgresql,下面是我用来将json插入数据库的代码 list=result['intervalsDataPoints'] for item in list: if item['dataPoints'] !=[]: result=item['dataPoints'] #print(result)

下面是我用来将json插入数据库的代码

list=result['intervalsDataPoints']
                for item in list:

                    if item['dataPoints'] !=[]:
                        result=item['dataPoints']
                        #print(result)

                        #print('hello')

                        for element in result:

                            dt = datetime.datetime.now()
                            #element["dt"] = dt
                            #element["epic"]=epic
                            #print(element)
                            #print(element['openPrice']['bid'])
                            print(element['timestamp'])
                            date_timestamp = datetime.datetime.fromtimestamp(element['timestamp']/1000)
                            cursor.execute("""INSERT INTO market_data_historic VALUES('%s','%s','%s',%s,%s,%s,%s,%s,%s,%s,%s,%s) ON CONFLICT ON CONSTRAINT UNIQUE_history DO NOTHING"""%(dt,epics,date_timestamp,element['openPrice']['bid'],element['openPrice']['ask'],element['closePrice']['bid'],element['closePrice']['ask'],element['highPrice']['bid'],element['highPrice']['ask'],element['lowPrice']['bid'],element['lowPrice']['ask'],element['lastTradedVolume']))
                            #cursor.executemany("""INSERT INTO market_data_historic(created_at,epic,timestamp,openprice_bid,openprice_close,closeprice_bid,closeprice_ask,highprice_bid,highprice_ask,lowprice_bid,lowprice_ask,last_traded_volume) VALUES (%(dt)s,%(epic)s,%(timestamp)s,%(openPrice['bid'])s,%(openPrice['close'])s,%(closePrice['bid'])s,%(closePrice['ask'])s,%(highPrice['bid'])s,%(highPrice['ask'])s,%(lowPrice['bid'])s,%(lowPrice['ask'])s,%(lastTradedVolume)s);""",element)
                            conn.commit()
                            #print(item)
但是,某些项可能为NULL,这会导致我的insert语句中断,从而导致此错误:

    cursor.execute("""INSERT INTO market_data_historic VALUES('%s','%s','%s',%s,%s,%s,%s,%s,%s,%s,%s,%s) ON CONFLICT ON CONSTRAINT UNIQUE_history DO NOTHING"""%(dt,epics,date_timestamp,element['openPrice']['bid'],element['openPrice']['ask'],element['closePrice']['bid'],element['closePrice']['ask'],element['highPrice']['bid'],element['highPrice']['ask'],element['lowPrice']['bid'],element['lowPrice']['ask'],element['lastTradedVolume']))
KeyError: 'bid'
我已尝试在每个元素的末尾追加或不追加,从而生成如下查询:

cursor.execute("""INSERT INTO market_data_historic VALUES('%s','%s','%s',%s,%s,%s,%s,%s,%s,%s,%s,%s) ON CONFLICT ON CONSTRAINT UNIQUE_history DO NOTHING"""%(dt,epics,date_timestamp,element['openPrice']['bid'] or None,element['openPrice']['ask'] or None,element['closePrice']['bid'] or None,element['closePrice']['ask'] or None,element['highPrice']['bid'] or None,element['highPrice']['ask'] or None,element['lowPrice']['bid'] or None,element['lowPrice']['ask'] or None,element['lastTradedVolume'] or None))
还是不走运!
你们建议我如何在上面的代码中说明NONE/NULL值,无论何时,当您试图访问python字典中的键的值时,使用这种方法
元素['highPrice']['bid']
,您必须确保键存在,否则将触发异常。 如果不确定密钥是否存在,则应使用以下命令:

element['highPrice'].get('bid')  
这不会引发异常,如果密钥不存在,默认情况下将返回None。如果密钥不存在,也可以提供所需的返回值,如下所示:

element['highPrice'].get('bid', 'value')

作为这里的一个注释,我假设您总是有“highPrice”键可用。如果不是相同的逻辑,那么也应该使用.get

每当您试图访问python字典中的某个键的值时,使用这种方法
元素['highPrice']['bid']
,您必须确保该键存在,否则将触发异常。 如果不确定密钥是否存在,则应使用以下命令:

element['highPrice'].get('bid')  
这不会引发异常,如果密钥不存在,默认情况下将返回None。如果密钥不存在,也可以提供所需的返回值,如下所示:

element['highPrice'].get('bid', 'value')

作为这里的一个注释,我假设您总是有“highPrice”键可用。如果不是相同的逻辑,那么也应该使用.get

Hi刚刚尝试看到错误:psycopg2.errors.UndefinedColumn:列“none”不存在第1行:…1.982000','CC.D.LCO.USS.IP','2019-11-07 23:00:00',none,none,…Hi,这对我来说很奇怪,在您发布的查询中,您没有指定列名,我相信任何none值都应该指向列的值。确保没有动态拾取列名,因为这将不起作用。甚至可以尝试在查询中指定列名并尝试一些调试。感谢您的响应。我已经按照你的建议做了:(“插入市场数据历史记录(创建时间、epic、时间戳、openprice出价、openprice出价、closeprice出价、closeprice出价、highprice出价、highprice出价、lowprice出价、lowprice出价、最后交易量)值-仍然看到相同的错误由于这不是某些人可以真正复制的,我只能给你一些建议来帮助你确定真正的问题在哪里。首先,在执行一个静态插入查询时,你知道它可以工作,并从脚本内部测试它是否成功。然后,为了调试你的查询,分配查询创建到一个变量,并在执行之前打印出来。通过这种方式,您可以获得查询并直接将其应用到数据库。可能有些列不能为空,可能有些数据没有到达正确的列。您需要使用脚本生成的数据分析查询。Dimitra Paraskevopoulou非常感谢您的支持帮助我完全按照您建议的方式打印查询以进行调试。结果表明,NONE不在PostgresSQL有问题的引号中。再次感谢!他刚刚尝试了查看错误:psycopg2.errors.UndefinedColumn:column“NONE”"不存在第1行:…1.982000','CC.D.LCO.USS.IP','2019-11-07 23:00:00',None,None,…您好,这对我来说很奇怪,在您发布的查询中,您没有指定列名,我相信任何None值都应该指向列的值。请确保没有动态拾取列名,因为这将不起作用。甚至可能尝试在查询中指定列名并尝试一些调试。感谢您回复Dimitra。我已按照您的建议执行了以下操作:(“插入市场数据”历史记录(创建时间、epic、时间戳、openprice出价、openprice出价、openprice出价、closeprice出价、closeprice出价、highprice出价、highprice出价、lowprice出价、lowprice出价、最后交易量)值-仍然看到相同的错误由于这不是某些人可以真正复制的,我只能给你一些建议来帮助你确定真正的问题在哪里。首先,在执行一个静态插入查询时,你知道它可以工作,并从脚本内部测试它是否成功。然后,为了调试你的查询,分配查询创建到一个变量,并在执行之前打印出来。通过这种方式,您可以获得查询并直接将其应用到数据库。可能有些列不能为空,可能有些数据没有到达正确的列。您需要使用脚本生成的数据分析查询。Dimitra Paraskevopoulou非常感谢您的支持帮助我完全按照您建议的那样打印查询以进行调试。结果表明,NONE不在PostgresSQL有问题的引号中。再次感谢!