Python Forum
Python output didn't write properly
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python output didn't write properly
#1
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.
Yoriz write Oct-22-2022, 11:41 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What is the error of not being able to pull data in this code? i didn't see an error? TestPerson 2 1,228 Sep-30-2022, 02:36 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020