如何覆盖 CsvItemExporter 的 join_multivalued 选项
How to override join_multivalued option of CsvItemExporter
CsvItemExporter 有一个选项 join_multivalued
,默认为逗号 (=','
)。
如何在我的 scrapy 项目中将其更改为另一个字符?
在您的自定义 CSVItemExporter
中这样设置:
from scrapy.conf import settings
from scrapy.contrib.exporter import CsvItemExporter
class CSVkwItemExporter(CsvItemExporter):
def __init__(self, *args, **kwargs):
kwargs['fields_to_export'] = settings.getlist('EXPORT_FIELDS') or None
kwargs['encoding'] = settings.get('EXPORT_ENCODING', 'utf-8')
super(CSVkwItemExporter, self).__init__(*args, **kwargs)
self._join_multivalued = settings.get('MY_CSV_DELIMITER', ',')
其中 MY_CSV_DELIMITER
是分隔符的示例设置。
Scrapy 版本 1.5.0
your_scrapy_project/exporters.py
from scrapy.conf import settings
from scrapy.exporters import CsvItemExporter
class MyCsvItemExporter(CsvItemExporter):
def __init__(self, *args, **kwargs):
delimiter = settings.get('CSV_DELIMITER', ';')
kwargs['delimiter'] = delimiter
super(MyCsvItemExporter, self).__init__(*args, **kwargs)
your_scrapy_project/settings.py
FEED_EXPORT_ENCODING = 'utf-8'
FEED_EXPORTERS = {
'csv': 'your_scrapy_project.exporters.MyCsvItemExporter',
}
CSV_DELIMITER = ';'
CsvItemExporter 有一个选项 join_multivalued
,默认为逗号 (=','
)。
如何在我的 scrapy 项目中将其更改为另一个字符?
在您的自定义 CSVItemExporter
中这样设置:
from scrapy.conf import settings
from scrapy.contrib.exporter import CsvItemExporter
class CSVkwItemExporter(CsvItemExporter):
def __init__(self, *args, **kwargs):
kwargs['fields_to_export'] = settings.getlist('EXPORT_FIELDS') or None
kwargs['encoding'] = settings.get('EXPORT_ENCODING', 'utf-8')
super(CSVkwItemExporter, self).__init__(*args, **kwargs)
self._join_multivalued = settings.get('MY_CSV_DELIMITER', ',')
其中 MY_CSV_DELIMITER
是分隔符的示例设置。
Scrapy 版本 1.5.0
your_scrapy_project/exporters.py
from scrapy.conf import settings
from scrapy.exporters import CsvItemExporter
class MyCsvItemExporter(CsvItemExporter):
def __init__(self, *args, **kwargs):
delimiter = settings.get('CSV_DELIMITER', ';')
kwargs['delimiter'] = delimiter
super(MyCsvItemExporter, self).__init__(*args, **kwargs)
your_scrapy_project/settings.py
FEED_EXPORT_ENCODING = 'utf-8'
FEED_EXPORTERS = {
'csv': 'your_scrapy_project.exporters.MyCsvItemExporter',
}
CSV_DELIMITER = ';'