I produce the site parser. I need to write data to the csv file in a stepwise manner (photo with an example below), but I write everything in one line. Tell me, how can I solve this problem?
import re import requests from bs4 import BeautifulSoup import csv class Tesco: def get_html(self, url): r = requests.get(url) return r.text def write_csv(self, data): with open('tesco.csv', 'a', newline='') as f: writer = csv.writer(f) writer.writerow(( data['category'], data['sub_category_1'], data['sub_category_2'], data['sub_category_3'], )) def get_page_data(self, html): main_soup = BeautifulSoup(html, 'lxml') groceries = main_soup.find('ul', class_='menu-superdepartment').find_all('li')[:-1] for grocery in groceries: category = (re.sub(r'Shop|department', '', grocery.find('span').text)).lower() category_url = 'https://www.tesco.com' + grocery.find('a').get('href') category_html = self.get_html(category_url) category_soup = BeautifulSoup(category_html, 'lxml') sub_groceries = category_soup.find('div', class_='current').find_all('li')[1:] for sub_grocery in sub_groceries: sub_category_1 = (re.sub(r"'", "", sub_grocery.find('span', class_='list-item-single-line').text)).lower() sub_category_1_url = 'https://www.tesco.com' + sub_grocery.find('a').get('href') sub_category_1_html = self.get_html(sub_category_1_url) sub_category_1_soup = BeautifulSoup(sub_category_1_html, 'lxml') sub_groceries_1 = sub_category_1_soup.find('div', class_='current').find_all('li')[1:] for sub_grocery_1 in sub_groceries_1: sub_category_2 = (re.sub(r"'", "", sub_grocery_1.find('span', class_='list-item-single-line').text)).lower() sub_category_2_url = 'https://www.tesco.com' + sub_grocery_1.find('a').get('href') sub_category_2_html = self.get_html(sub_category_2_url) sub_category_2_soup = BeautifulSoup(sub_category_2_html, 'lxml') try: check = sub_category_2_soup.find('div', class_='empty-section').find('span', class_='icon-info2') except: check = '' if check == '': sub_groceries_2 = sub_category_2_soup.find('div', class_='filter-options')\ .find_all('li', class_='filter-option__container') for sub_grocery_2 in sub_groceries_2: sub_category_3 = sub_grocery_2.find('label', class_='checkbox-label').get('title').lower() data = { 'category': category, 'sub_category_1': sub_category_1, 'sub_category_2': sub_category_2, 'sub_category_3': sub_category_3, } self.write_csv(data) def main(): tesco = Tesco() url = 'https://www.tesco.com/groceries/en-GB' tesco.get_page_data(tesco.get_html(url)) if __name__ == '__main__': main() 

csvmodule is designed to work with CSV files. What you are trying to do is not a CSV file - reading (parsing) it as a CSV will be very difficult. - MaxU