首页 > 其他分享 ><1> csv 存储

<1> csv 存储

时间:2022-11-02 16:12:00浏览次数:47  
标签:__ 存储 self item csv class def

"""方式一(scrapy下):"""
scrapy crawl 爬虫名 -o 保存的csv文件名

"""方式二(常用):"""
from scrapy.exporters import CsvItemExporter
class CsvPipeline:

    def __init__(self):
        # 文件存储初始化操作
        self.file = open('filename.csv', 'wb')
        self.exporter = CsvItemExporter(file=self.file, include_headers_line=True)
        self.exporter.start_exporting()

    def process_item(self, item, spider):
        self.exporter.export_item(item)
        return item

    def close_spider(self, spider):
        self.exporter.finish_exporting()
        self.file.close()

"""方式三(写入列表到 CSV)"""    
import csv
class Snippet:

    def __init__(self):
        self.f = open('filename.csv', 'a', encoding='utf-8', newline='')
        self.wirter = csv.writer(self.f)

    def writercsv(self, data):
        '''
        写入数据到 csv 文件
        :param data: 输入数据 [item1, item2, ..., itemN]
        :return:
        '''
        Ellipsis
        self.wirter.writerow(data)

    def __del__(self):
        self.f.close()
        

"""方式四(写入字典到 CSV)"""     
import csv
class Snippet:

    def __init__(self):
        self.f = open('filename.csv', 'a', encoding='utf-8', newline='')
        # 写入表头
        self.headers = ['class', 'name', 'sex', 'height', 'year']
        self.dictwirter = csv.DictWriter(self.f, self.headers)
        self.dictwirter.writeheader() # 写入表头

    def writercsv(self):
        dict_rows = [
            {'class': 1, 'name': 'xiaoming', 'sex': 'male', 'height': 168, 'year': 23},
            {'class': 2, 'name': 'xiaoli', 'sex': 'male', 'height': 158, 'year': 21},
        ]
        for row in dict_rows:
            self.dictwirter.writerow(row) # 写入字段数据 字典 格式

    def __del__(self):
        self.f.close()


if __name__ == '__main__':
    snippet = Snippet()
    snippet.dosomething()


 

标签:__,存储,self,item,csv,class,def
From: https://www.cnblogs.com/modly/p/16851356.html

相关文章