Home > Software engineering >  For loop working and read data but did not get write on csv file
For loop working and read data but did not get write on csv file

Time:09-18

When running this code everything works but the output did not write on the CSV file.

import csv
import requests
from recipe_scrapers import scrape_html
from csv import writer

with open('recipe.csv', 'w', encoding='utf8', newline='') as file:
    thewriter = writer(file)
    header = ['Title', 'Ingredients', 'Instructions', 'Nutrition_Facts', 'image', 'links']
    thewriter.writerow(header)

    with open('data.txt', 'r') as inf:
        for line in inf:
            html = requests.get(line).content
            scraper = scrape_html(html=html, org_url=line)
thewriter.writerow([scraper.title(), scraper.ingredients(), scraper.instructions(), scraper.nutrients(), scraper.image(), scraper.links()])

CodePudding user response:

If the indentation of your code is as you show it, you only write the header to the output file, loop through the input, and then only try to write one more line from the last scraper, but even that is after the output file has been closed when you exited the with block.

Indent the last line to the same level as scraper = ... or there's no chance of this working properly. Like this:

with open('recipe.csv', 'w', encoding='utf8', newline='') as file:
    thewriter = writer(file)
    ...

    with open('data.txt', 'r') as inf:
        for line in inf:
            html = requests.get(line).content
            scraper = scrape_html(html=html, org_url=line)
            thewriter.writerow([.....])     # <---- proper indent

CodePudding user response:

I would try rearranging the logic so it is more orderly. This could help when debugging your code in the future.

As alexis pointed out your problem was improper indentation causing a scope issue. I would revisit some training material of your choice that speaks on scope in Python.

from csv import writer

import requests
from recipe_scrapers import scrape_html

with open('recipe.csv', 'w', encoding='utf8', newline='') as file:
    thewriter = writer(file)
    thewriter.writerow(['Title', 'Ingredients', 'Instructions', 'Nutrition_Facts', 'image', 'links'])  # Header
    
    with open('data.txt', 'r') as inf:
        for line in inf:
            html = requests.get(line).content
            scraper = scrape_html(html=html, org_url=line)
            
            thewriter.writerow([scraper.title(), scraper.ingredients(), scraper.instructions(), scraper.nutrients(), scraper.image(), scraper.links()])
  • Related