Python Forum

Full Version: Python output didn't write properly
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
import requests
from subprocess import list2cmdline
import csv
import json

html_output = []
names = []

def pylist_to_list(list):
    temp_list = ['<ul>']
    for element in list:
        temp_list.append(f'<li>{element}</li>')

    temp_list.append('</ul>')
    html_string = ''.join(temp_list)
    return html_string


# This 'my_list.csv' file have bunch line of data list
with open('my_list.csv', 'r') as data_file:
    csv_data = csv.DictReader(data_file)
    # Ingredients is header name
    for line in csv_data:
        names.append(f"{line['Ingredients']}")

for name in names:
    name = name.replace(']', '').replace('[', '').replace('\'', '"')
    ingredient_list = name.split('"')

    while '' in ingredient_list:
        ingredient_list.remove('')

    while ', ' in ingredient_list:
        ingredient_list.remove(', ')

    single_element = pylist_to_list(ingredient_list)
    html_output.append(single_element)
    
#for row in html_output:
    with open('final.csv', 'w', encoding='utf8', newline='') as f:
        writer = csv.writer(f)
        header = ['Ingredients']
        writer.writerow(header)
         writer.writerow(html_output)

print(html_output[0])
When running this script then it writes the first row but I want to write more rows. How to fix it.
The indentation for the loop where you're writing the data to the final.csv file seems to be incorrect. Additionally, you are overwriting the final.csv file for each row, which results in only the last row being present in the file. To fix these issues, you can update your script as follows:

import csv

html_output = []

def pylist_to_list(lst):
    temp_list = ['<ul>']
    for element in lst:
        temp_list.append(f'<li>{element}</li>')
    temp_list.append('</ul>')
    html_string = ''.join(temp_list)
    return html_string

names = []

with open('my_list.csv', 'r') as data_file:
    csv_data = csv.DictReader(data_file)
    for line in csv_data:
        names.append(f"{line['Ingredients']}")

for name in names:
    name = name.replace(']', '').replace('[', '').replace('\'', '"')
    ingredient_list = name.split('"')

    while '' in ingredient_list:
        ingredient_list.remove('')

    while ', ' in ingredient_list:
        ingredient_list.remove(', ')

    single_element = pylist_to_list(ingredient_list)
    html_output.append(single_element)

with open('final.csv', 'w', encoding='utf8', newline='') as f:
    writer = csv.writer(f)
    header = ['Ingredients']
    writer.writerow(header)
    for row in html_output:  # Iterate through html_output list and write each row
        writer.writerow([row])

print("Data has been written to 'final.csv'.")
This code should correctly process each row from the my_list.csv file and write the HTML-formatted rows to the final.csv file. Each row will be correctly written on a new line in the output CSV file.