Python Scrapy:item.\u值和item.fields之间有什么区别?

Python Scrapy:item.\u值和item.fields之间有什么区别?,python,scrapy,Python,Scrapy,(使用Scrapy 1.0.0):我声明了一个包含三个字段的项: class MyItem(scrapy.Item): # define the fields for your item here correct_ans = scrapy.Field() pronunciation = scrapy.Field() good_alts

(使用Scrapy 1.0.0):我声明了一个包含三个字段的项:

class MyItem(scrapy.Item):
    # define the fields for your item here                                                                 
    correct_ans = scrapy.Field()
    pronunciation = scrapy.Field()
    good_alts = scrapy.Field()

def __init__(self, correct_ans, pronunciation):
    # bad style??                                                                                      
    super(MyItem, self).__init__()
    self._values['correct_ans'] = correct_ans
    self._values['pronunciation'] = pronunciation
    self._values['good_alts'] = []
奇怪的是,在我实例化一个新的MyItem之后,self.\u值和self.field有不同的键(参见下面的pdb输出)。问题是。。。为什么?这是故意的吗

(Pdb) l
 24         def parse(self, response):
 25             for vocab in self.vocabs:
 26                 item = MyItem(vocab['correct_ans'], vocab['pronunciation'])
 27                 pdb.set_trace()
 28  ->             request = FormRequest.from_response(response, formnumber=1, 
 29                                                     formdata = {'word': item['pronunciation']}, 
 30                                                     callback =     self.parse_pinyin_match_page)
 31                 logging.debug("OK:" + str(request))
 32                 request.meta['item'] = item
 33                 yield request
(Pdb) item.fields
{'correct_ans': {}, 'pronunciation': {}}
(Pdb) item._values
{'correct_ans': '\xe9\x99\x88\xe5\xa4\xa7\xe4\xb8\x9c', 'pronunciation': 'chendadong', 'good_alts': []}
(Pdb) 'good_alts' in item.fields
False
(Pdb) 'good_alts' in item._values
True
(Pdb) 
在我尝试使用内置的CSV导出器之前,我没有遇到任何问题。现在,scrapy退出,并出现以下错误:

  File "/Library/Python/2.7/site-packages/scrapy/exporters.py", line 188, in export_item
    values = [x[1] for x in fields]
  File "/Library/Python/2.7/site-packages/scrapy/exporters.py", line 72, in _get_serialized_fields
    field = {} if isinstance(item, dict) else item.fields[field_name]
KeyError: 'good_alts'
我找到了答案

我还定义了一个函数:

def good_alts():
    return self._values['good_alts']

删除此函数修复了该问题。因此,出现了某种名称冲突。这仍然是一个非常令人困惑的错误,但我应该知道不要对函数和属性使用相同的名称。

您使用的是哪个版本的scrapy?键入“scrapy version”会得到响应“scrapy 1.0.0”