我有很多python列表,我将数据传输到MySQL数据库。 从技术上讲,每个列表变量各有60个值。但是在执行for循环时,只有1条记录被更新而不是60条。我使用python zip表格很长时间并且从未遇到过问题。
flg=1
prodcat="sac"
for a,b,c,d,e,f,g,h,o,p,q,r,s,t,ab,ac,ad,ae,af,ag,ah,ai,aj,ak,al,am,an,ao,ap,aq,ar,as1,at1,au in zip(lipgurl,lipdate,lides,liref,liname,licontry,listype,liprice,licurcy,lim,licolor,licond,lisize,liwidth,liheight,lidepth,liwish,limsg,liselracc,librand,liuoid,lifolby,lifolwing,lilikes,lisolddt,libuyer,liusprice,lipbought,libuyernm,libuyacc,libuycont,libuytype,liselrid,lisrcpage):
myid="VC"+str(flg)
cur.execute("INSERT INTO details VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",(myid,a,au,ag,rdate,b,prodcat,c,d,e,at1,f,g,h,o,an,p,q,r,s,t,ab,ac,ad,ae,af,ai,aj,ak,al,am,ao,ap,aq,ar,as1))
flg=flg+1
print("Database updated")
con.commit()
为了检查每个列表变量中是否有60个值,我在上面的循环中插入一个循环并打印出来。它打印出所有60个值。
我哪里错了?有什么建议吗?
答案 0 :(得分:2)
很可能你的列表中只包含一个元素,所以zip只返回一个元组,使用itertools.zip_longest代替zip来解决你的问题(你会得到填充的项目拉链......):
from itertools import zip_longest
flg=1
prodcat="sac"
for a,b,c,d,e,f,g,h,o,p,q,r,s,t,ab,ac,ad,ae,af,ag,ah,ai,aj,ak,al,am,an,ao,ap,aq,ar,as1,at1,au\
in zip_longest(lipgurl,lipdate,lides,liref,liname,licontry,listype,liprice,licurcy,lim,
licolor,licond,lisize,liwidth,liheight,lidepth,liwish,limsg,liselracc,librand,
liuoid,lifolby,lifolwing,lilikes,lisolddt,libuyer,liusprice,lipbought,libuyernm,
libuyacc,libuycont,libuytype,liselrid,lisrcpage):
myid="VC"+str(flg)
cur.execute("INSERT INTO details VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",(myid,a,au,ag,rdate,b,prodcat,c,d,e,at1,f,g,h,o,an,p,q,r,s,t,ab,ac,ad,ae,af,ai,aj,ak,al,am,ao,ap,aq,ar,as1))
flg=flg+1
print("Database updated")
con.commit()