简体   繁体   English

无法将Scrapy项目用作MySQL.execute的数据:“字符串格式化期间参数数量错误”

[英]Unable to use Scrapy item as data for MySQL.execute: “Wrong number of arguments during string formatting”

I'm using Scrapy to parse content from a website into Items , which replicate the dict API: 我正在使用Scrapy将网站中的内容解析为Items ,这些内容复制了dict API:

import scrapy

class ScheduleItem(scrapy.Item):
    flight = scrapy.Field()

I'm trying to use the Item above directly as the data for a Connector/Python MySQLCursor.execute() statement using pyformat-style values. 我试图将上面的项目直接用作使用pyformat样式值的Connector / Python MySQLCursor.execute()语句的数据。 However, it's falling over with the following error: 但是,它因以下错误而崩溃:

(Pdb) add_schedule_sql()
'INSERT INTO schedules (flight) VALUES (%(flight)s)'

(Pdb) foo = items.ScheduleItem();
(Pdb) foo['flight'] = 'abc'
(Pdb) foo.keys()
['flight']
(Pdb) print foo
{'flight': 'abc'}
(Pdb) self.cursor.execute(add_schedule_sql(), foo)
*** ProgrammingError: Wrong number of arguments during string formatting

There's exactly one value to insert and exactly one value in the Item, both with the same key, so I'm rather baffled by this error. 有一个要插入的值和一个项目中有一个值,两者都使用相同的键,因此我对此错误感到困惑。 ( Source code here , which indicates it's actually hiding a TypeError.) If I use a plain old dict, it works fine: 此处的源代码表示它实际上隐藏了TypeError。)如果我使用普通的旧字典,则可以正常工作:

(Pdb) bar = {'flight': 'abc'}
(Pdb) bar.keys()
['flight']
(Pdb) self.cursor.execute(add_schedule_sql(), bar)
(Pdb)

And if I map the item into a dict, the call also works fine: 如果我将项目映射到字典中,该调用也可以正常工作:

(Pdb) self.cursor.execute(add_schedule_sql(), dict(item))
(Pdb)

The above is simple enough that it actually solves my problem adequately, but I'm still curious about what's wrong with just using an Item. 上面的内容很简单,它实际上可以很好地解决我的问题,但是我仍然对仅使用Item有什么问题感到好奇。 Python 2.7.5, Scrapy 0.24 (latest stable). Python 2.7.5,Scrapy 0.24(最新稳定版)。

The following works for me. 以下对我有用。 So we need to see your implementation of ScheduleItem and also we need the version numbers of the libraries involved. 因此,我们需要查看您的ScheduleItem实现,并且还需要所涉及的库的版本号。

In [10]: class ScheduleItem(scrapy.Item):
flight = scrapy.Field()
delay = scrapy.Field()
....:     

In [11]: foo = ScheduleItem()

In [12]: foo['flight'] = 'abc'

In [13]: foo
Out[13]: {'flight': 'abc'}

In [14]: "test %(flight)s str"%(foo)
Out[14]: 'test abc str'

In [15]: "test (flight) and %(flight)s str"%(foo)
Out[15]: 'test (flight) and abc str'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ProgrammingError:字符串格式化过程中的参数数量错误 - ProgrammingError: Wrong number of arguments during string formatting mysql.connector.errors.ProgrammingError:字符串格式化期间参数数量错误 - mysql.connector.errors.ProgrammingError: Wrong number of arguments during string formatting 编程错误:字符串格式化期间的参数数量错误,尝试过元组修复 - ProgrammingError: Wrong number of arguments during string formatting, tried tuple fix TypeError:并非所有使用sql格式化字符串期间转换的参数都在Python中执行 - TypeError: not all arguments converted during string formatting with sql execute in Python 我尝试向MySQL插入数据时出现此错误 - >“TypeError:并非在字符串格式化期间转换所有参数” - Getting this error while I try to insert data to MySQL->“TypeError: not all arguments converted during string formatting” TypeError:并非使用MySQL和Python在字符串格式化期间转换所有参数 - TypeError: not all arguments converted during string formatting using MySQL and Python flask-mysql编程错误:字符串格式化期间并非所有参数都已转换 - flask-mysql ProgrammingError: not all arguments converted during string formatting MySQL + Python:在字符串格式化期间并非所有参数都被转换 - MySQL + Python: Not all arguments converted during string formatting MySQL数据库调用:在字符串格式化期间,并非所有参数都已转换 - MySQL db call: not all arguments converted during string formatting TypeError:并非在字符串格式化过程中转换了所有参数[MySQL DB连接] - TypeError: not all arguments converted during string formatting [MySQL DB connection]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM