Declaring Item Loaders
Item Loaders are declared by using a class definition syntax. Here is an example:
from itemloaders import ItemLoader
from itemloaders.processors import TakeFirst, MapCompose, Join
class ProductLoader(ItemLoader):
default_output_processor = TakeFirst()
name_in = MapCompose(str.title)
name_out = Join()
# using a built-in processor
price_in = MapCompose(str.strip)
# using a function
def price_out(self, values):
return float(values[0])
loader = ProductLoader()
loader.add_value('name', 'plasma TV')
loader.add_value('price', '999.98')
loader.load_item()
# {'name': 'Plasma Tv', 'price': 999.98}
As you can see, input processors are declared using the _in
suffix while
output processors are declared using the _out
suffix. And you can also
declare a default input/output processors using the
ItemLoader.default_input_processor
and
ItemLoader.default_output_processor
attributes.
The precedence order, for both input and output processors, is as follows:
Item Loader field-specific attributes:
field_in
andfield_out
(most precedence)Field metadata (
input_processor
andoutput_processor
keys).Check out itemadapter field metadata for more information.
Added in version 1.0.1.
Item Loader defaults:
ItemLoader.default_input_processor()
andItemLoader.default_output_processor()
(least precedence)
See also: Reusing and extending Item Loaders.