How to get field order in Scrapy element

I'm interested in a reference to the order of the field names in the scrapy element. where is it stored

>>> dir(item) Out[7]: ['_MutableMapping__marker', '__abstractmethods__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dict__', '__doc__', '__eq__', '__format__', '__getattr__', '__getattribute__', '__getitem__', '__hash__', '__init__', '__iter__', '__len__', '__metaclass__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '__weakref__', '_abc_cache', '_abc_negative_cache', '_abc_negative_cache_version', '_abc_registry', '_class', '_values', 'clear', 'copy', 'fields', 'get', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values'] 

I tried item.keys () but this returns an unordered dict

0
source share
1 answer

Item class has a dict interface that stores values ​​in _values dict, which does not track the order of keys ( https://github.com/scrapy/scrapy/blob/master/scrapy/item.py#L50 ). I believe that you could subclass from Item and override the __init__ method to make this container Ordereddict :

 from scrapy import Item from collections import OrderedDict class OrderedItem(Item): def __init__(self, *args, **kwargs): self._values = OrderedDict() if args or kwargs: # avoid creating dict for most common case for k, v in six.iteritems(dict(*args, **kwargs)): self[k] = v 

Then the element saves the order in which the values ​​were assigned:

 In [28]: class SomeItem(OrderedItem): ...: a = Field() ...: b = Field() ...: c = Field() ...: d = Field() ...: ...: i = SomeItem() ...: i['b'] = 'bbb' ...: i['a'] = 'aaa' ...: i['d'] = 'ddd' ...: i['c'] = 'ccc' ...: i.items() ...: Out[28]: [('b', 'bbb'), ('a', 'aaa'), ('d', 'ddd'), ('c', 'ccc')] 
+6
source

Source: https://habr.com/ru/post/1261157/


All Articles