The following code works sometimes, it pushes it to the directory it needs to be in and IT always copy the file, however sometimes there is no data in the csv files in the leadparser directory.
import csv
import re
import os
import shutil
class myExporter(object):
def __init__(self):
self.i = 0
self.filename = 'output%s.csv'
self.srcfile = '/Users/poweruser/Applications/pythonwork/bbbscrap2/scrape/' + self.filename
while os.path.exists(self.srcfile % self.i):
self.i += 1
self.folderdes = '/Users/poweruser/Applications/pythonwork/leadparser/newfiles'
self.myCSV = csv.writer(open(self.filename % self.i, 'w'))
self.myCSV.writerow(['Email', 'Website', 'Phone Number', 'Location'])
def process_item(self, item, spider):
self.myCSV.writerow([item['email'],
item['website'],
item['phonenumber'],
item['location']])
self.folderPath = os.path.join(
self.folderdes, os.path.basename(self.srcfile % self.i))
shutil.copy(self.srcfile % self.i, self.folderPath)
return item
You have to close the file, to get all data written:
import csv
import re
import os
import shutil
SRCFILE = '/Users/poweruser/Applications/pythonwork/bbbscrap2/scrape/output%s.csv'
DESTINATION_FOLDER = '/Users/poweruser/Applications/pythonwork/leadparser/newfiles'
class myExporter(object):
def __init__(self):
i = 0
while os.path.exists(SRCFILE % i):
i += 1
self.filename = SRCFILE % i
with open(self.filename, 'w') as output:
output = csv.writer(output)
output.writerow(['Email', 'Website', 'Phone Number', 'Location'])
def process_item(self, item, spider):
with open(self.filename, 'a') as output:
output = csv.writer(output)
output.writerow([item['email'],
item['website'],
item['phonenumber'],
item['location']])
folder = os.path.join(DESTINATION_FOLDER, os.path.basename(self.filename))
shutil.copy(self.filename, folder)
return item
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.