python 對csv檔案操作
阿新 • • 發佈:2018-12-11
# scrapy pipline class BazaarPipeline(object): def open_spider(self, spider): self.filename = open("digital.csv", "wb") # 建立一個csv檔案讀寫物件,引數是需要儲存資料的csv檔案物件 self.csv_exporter = CsvItemExporter(self.filename) # 表示開始進行資料寫入 self.csv_exporter.start_exporting() def process_item(self, item, spider): self.csv_exporter.export_item(item) return item def close_spider(self, spider): # 表示結束資料寫入 self.csv_exporter.finish_exporting() self.filename.close() # 其他檔案對csv檔案操作 # 讀取檔案 import csv import re #讀取檔案 csv_file = csv.reader(open('digital.csv','r')) print(csv_file) for data in csv_file: # 可用下標 獲取相對應的資料 # print(data[5]) # 寫入檔案 qq_list = ['xxx','xxx'] QQ_list = ['xxxx','xxx'] content = open('number.csv','a', newline='') csv_write = csv.writer(out,dialect='excel') csv_write.writerow(QQ_list) csv_write.writerow(qq_list) print ("ok")