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


Messages In This Thread
Python output didn't write properly - by mdmsd2 - Oct-22-2022, 10:06 AM

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,284 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