简体   繁体   中英

How to change csv exporter separator in Scrapy to semicolon?

I'm new in Scrapy. I have read several discussions about this tool. I have a problem exporting csv files. I'm scrapping numeric values with commas. The default separatos of csv exporter is comma, so I have some problems when I open the resulting file in Excel.

How can I change the default delimitor of csv files in Scrapy to semicolon? I read some discussions about this issue but I don't know what and where i have to add code.

Thanks in advance!

scraper/exporters.py

from scrapy.exporters import CsvItemExporter


class CsvCustomSeperator(CsvItemExporter):
    def __init__(self, *args, **kwargs):
        kwargs['encoding'] = 'utf-8'
        kwargs['delimiter'] = '╡'
        super(CsvCustomSeperator, self).__init__(*args, **kwargs)

scraper/settings.py

FEED_EXPORTERS = {
    'csv': 'scraper.exporters.CsvCustomSeperator'
}

This solution worked for me.

You should check if quotechar is enabled and set in your export.

https://doc.scrapy.org/en/latest/topics/spiders.html?highlight=CSV_DELIMITER#csvfeedspider-example

Usually text should be enquoted with " so it's no issue that the delimiter is in the text.

尝试这个:

scrapy crawl yourCrawlerName -o output.csv --set delimiter=";"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM