I have a scraper for yellow pages and after scraping from desired categories it saves all the data in a csv named parent.csv. It has a column keyword which has the category for the business listed. I want to separate different categories based on the keyword and generate different csv files for each. I have implemented the following in spider_closed function:
def spider_closed(self, spider):
with open('parent.csv', 'r') as file:
reader = csv.reader(file)
headers = next(reader, None)
next(reader, None)
for row in reader:
with open('{}.csv'.format(row[0]), 'a') as f:
writer = csv.writer(f)
writer.writerow(row)
With this i have been able to successfully separate the categories but the problem is with headers. I want the headers to be also written to each new file. Moreover the data in the new csv files have one space in each row. I need to solve both of these problems. Any help in this regard will be appreciated.
[![This is the parent.csv file that is generated by the spider successfully][1]][1]
[![then seperate the enteries based on the keyword and make a new csv file based pm that keyword. For example: all the data with go karts keywords must be in go karts.csv and so on][2]][2]
Parent.csv [1]: https://i.stack.imgur.com/Ucgym.png seperated.....go karts.csv [2]: https://i.stack.imgur.com/3NVKo.png