执行许多在运行 Python 代码时需要长时间和"killed"状态


myrows = []
for item in data:
try:
id= (item.get("data").get("id"))
except AttributeError:
id=''
accounts = [(act["Account"]) for act in item.get("data", {}).get("mainAccounts", [])]

rows=(id,accounts)
myrows.extend(rows)
cursor.executemany(insert_statement,myrows)  
connection_target.commit()

accounts返回一个类似于[00123','123','234','567']的列表。在此之前,只有帐户的价值,没有任何问题。我正试图在oracle表中逐行插入值,如下所示

id account
1  0123
1  123
1  234
1  567

出于某种原因,当我运行代码时,代码被卡住了,我得到了"被杀死";终端上的状态。你知道为什么执行死刑需要很长时间吗?或者有更好的方法写这篇文章吗?

提前感谢

您没有正确创建myrows。它需要为要创建的每一行都有一个单独的元素。您正在将所有帐户放入嵌套列表中。

您可以在列表理解中创建包含id的元组。

myrows = []
for item in data:
id = item.get("data", {}).get("id", '')
accounts = [(id, act["Account"]) for act in item.get("data", {}).get("mainAccounts", [])]

myrows.extend(accounts)

当设置id而不是try/except时,也可以使用get(),就像在accounts列表理解中一样。

最新更新