从Python for循环插入到mysql时出错 [英] error while inserting into mysql from python for loop

查看:185
本文介绍了从Python for循环插入到mysql时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:


这个脚本是否正确。我想直接在mysql数据库中插入h2,h3,meta数据。下面的代码无法正常工作。有人可以提供一个解决方案。

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ def $响应)
sites = hxs.select('// ul / li')
items = [site.select('// h2')。extract()]
item = [site .select('// h3')。extract()]
item1 = [site.select('// meta')。extract()]
for index,index1,index2 in range(len (item)),range(len(item)),range(len(item1)):
con = MySQLdb.connect($ b $ host =localhost,
user =dreamriks ,
passwd =dreamriks,
db =scraped_data

cur = con.cursor()
str = items [index]
str1 = item [index1]
str2 = item1 [index2]
cur.execute(插入头部(h2,h3,meta)值(%s,%s,%s) ,(str,str1,str2))
con.commit()
con.close()

出现的错误是:

 索引,索引1,索引2在范围内(len(items)) ,范围(len(item)),range(len(item1)):
exceptions.ValueError:需要多个值来解开


看起来你的列表中只有一个元素在其中,这是造成这个问题的原因。
请检查所有列表:

  items = [site.select('// h2')。extract )] 
item = [site.select('// h3')。extract()]
item1 = [site.select('// meta')。extract()]

确保它们符合预期。

 (len(item)),range(len(item)),range(len(item1))


这个语法一次遍历所有的列表,如果任何一个列表中的任何一个不匹配,就会引发错误值

为了更好的理解你的问题,请看下面的内容:

$ $ $ $ $ $ $ $ $ $ $ ,2,3]

在[2]中:l2 = [4,5,6]

在[3]中:l3 = [7]

在[4]中:对于范围(len(l1)),范围(len(l2)),范围(len(l3))中的index,index1,index2:
....:print嗨
....:
....:
Hi
Hi
---------------- -------------------------------------------------- ---------
ValueError Traceback(最近一次调用最后一次)

/ home / avasal /< ipython console> in< module>()

ValueError:需要多个值来解开



如果可能的话,你可以试试这个:

  for index,index1,index2 in zip(range(len(items)) ,范围(len(item)),range(len(item1)))


Possible Duplicate:
convert list to string to insert into my sql in one row in python scrapy

Is this script correct. I want to insert the scraped out h2,h3,meta data directly into mysql database. The code below does not work correctly. Can someone please provide a solution to this. I think the problem is with the for loop.

    def parse(self, response):
    hxs = HtmlXPathSelector(response)
    sites = hxs.select('//ul/li')
         items = [site.select('//h2').extract()]
         item = [site.select('//h3').extract()]
         item1 = [site.select('//meta').extract()]
    for index,index1,index2 in range (len( items)),range(len(item)),range(len(item1)):
         con = MySQLdb.connect(
                    host="localhost",
                    user="dreamriks",
                    passwd="dreamriks",
                    db="scraped_data"
                 )
         cur = con.cursor()
         str  = items[index]
         str1 = item[index1]
         str2 = item1[index2]
         cur.execute("""Insert into heads(h2,h3,meta) Values(%s,%s,%s)""",(str,str1,str2))
         con.commit()
         con.close()

The error that comes is:

 for index,index1,index2 in range (len( items)),range(len(item)),range(len(item1)):
 exceptions.ValueError: need more than 1 value to unpack

解决方案

It seems one of your list has only one element in them, which is causing the problem. Please check all the lists :

 items = [site.select('//h2').extract()]
 item = [site.select('//h3').extract()]
 item1 = [site.select('//meta').extract()]

Make sure they are as expected.

for index,index1,index2 in range (len( items)),range(len(item)),range(len(item1))

this syntax iterates over all the lists at once, if any of the len of lists don't match, value error will be raised,

For Better understanding of your problem see below:

In [1]: l1 = [1,2,3]

In [2]: l2 = [4,5,6]

In [3]: l3 = [7]

In [4]: for index,index1,index2 in range (len( l1)),range(len(l2)),range(len(l3)):
   ....:     print "Hi"
   ....:     
   ....:     
Hi
Hi
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)

/home/avasal/<ipython console> in <module>()

ValueError: need more than 1 value to unpack

can you try this if possible:

for index,index1,index2 in zip(range (len( items)),range(len(item)),range(len(item1)))

这篇关于从Python for循环插入到mysql时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆