コード例 #1
0
 def _exporter_for_item(self, item):
     product_name = item['name'][0]
     exporter = CsvItemExporter(self.product_file,
                                include_headers_line=False)
     exporter.fields_to_export = ['name', 'price', 'availability']
     exporter.start_exporting()
     self.product_to_exporter[product_name] = exporter
     return self.product_to_exporter[product_name]
コード例 #2
0
    def get_exporter(self, item):
        """
		Finds / creates an exporter for a given item
		Takes item as an argument and returns an exporter
		:param item: Pass an item to get its exporter
		"""
        #  Extract key from item using ItemAdapter
        adapter = ItemAdapter(item=item)
        key = adapter[self.key_field]

        # Create an exporter for the key, if needed
        # Check if the key doesn't exist in the dictionary
        if (not (key in self.exporters)):
            # Open a CSV file to create a new exporter
            exporter = CsvItemExporter(open(
                file=f'output/{self.out_dir}/{key}.csv', mode='ab'),
                                       include_headers_line=False)

            # Construct the header row from self.fields
            header = {
                self.fields[i]: self.fields[i]
                for i in range(0, len(self.fields))
            }

            # Configure the fields to export
            exporter.fields_to_export = self.fields

            # Start exporting the file
            exporter.start_exporting()

            # Export the header row
            exporter.export_item(item=header)

            # Add exporter to the dictionary
            self.exporters[key] = exporter

        # Return the corresponding exporter
        return self.exporters[key]