CsvItemExporter有一个选项join_multivalued
,默认为逗号(=','
)。
如何在scrapy项目中将其更改为另一个char?
答案 0 :(得分:2)
以这种方式将其设置为自定义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
是分隔符的示例设置。
答案 1 :(得分:0)
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)
FEED_EXPORT_ENCODING = 'utf-8'
FEED_EXPORTERS = {
'csv': 'your_scrapy_project.exporters.MyCsvItemExporter',
}
CSV_DELIMITER = ';'