Can't get rid of blank rows in csv output

后端 未结 1 1098
星月不相逢
星月不相逢 2020-12-17 08:13

I\'ve written a very tiny script in python scrapy to parse name, street and phone number displayed across multiple pages from yellowpage website. When I run my script i find

相关标签:
1条回答
  • 2020-12-17 08:39

    You can fix it by creating a new FeedExporter. Change your settings.py as below

    FEED_EXPORTERS = {
        'csv': 'project.exporters.FixLineCsvItemExporter',
    }
    

    create a exporters.py in your project

    exporters.py

    import io
    import os
    import six
    import csv
    
    from scrapy.contrib.exporter import CsvItemExporter
    from scrapy.extensions.feedexport import IFeedStorage
    from w3lib.url import file_uri_to_path
    from zope.interface import implementer
    
    
    @implementer(IFeedStorage)
    class FixedFileFeedStorage(object):
    
        def __init__(self, uri):
            self.path = file_uri_to_path(uri)
    
        def open(self, spider):
            dirname = os.path.dirname(self.path)
            if dirname and not os.path.exists(dirname):
                os.makedirs(dirname)
            return open(self.path, 'ab')
    
        def store(self, file):
            file.close()
    
    
    class FixLineCsvItemExporter(CsvItemExporter):
    
        def __init__(self, file, include_headers_line=True, join_multivalued=',', **kwargs):
            super(FixLineCsvItemExporter, self).__init__(file, include_headers_line, join_multivalued, **kwargs)
            self._configure(kwargs, dont_fail=True)
            self.stream.close()
            storage = FixedFileFeedStorage(file.name)
            file = storage.open(file.name)
            self.stream = io.TextIOWrapper(
                file,
                line_buffering=False,
                write_through=True,
                encoding=self.encoding,
                newline="",
            ) if six.PY3 else file
            self.csv_writer = csv.writer(self.stream, **kwargs)
    

    I am on Mac, so can't test its windows behavior. But if above doesn't work then change below part of code and set newline="\n"

            self.stream = io.TextIOWrapper(
                file,
                line_buffering=False,
                write_through=True,
                encoding=self.encoding,
                newline="\n",
            ) if six.PY3 else file
    
    0 讨论(0)
提交回复
热议问题