1

The break here is bothering me; after extensive research I want to ask if there is a pythonic way to convert this to a while loop:

import re

file = open('parse.txt', 'r')
html = file.readlines()

    def cleanup():
    result = []
    for line in html:
        if "<li" and "</li>" in line:
            stripped = re.sub(r'[\n\t]*<[^<]+?>', '', line).rstrip()
            quoted = f'"{stripped}"'
            result.append(quoted)
        elif "INSTRUCTIONS" in line:
            break
    return ",\n".join(result)

I really am trying to practice designing more efficient loops.

added parse.txt

<p style="text-align:justify"><strong><span style="background-color:#ecf0f1">INGREDIENTS</span></strong></p>

    <li style="text-align:justify"><span style="background-color:#ecf0f1">3 lb ground beef (80/20)</span></li>
<ul>
    <li style="text-align:justify"><span style="background-color:#ecf0f1">1 large onion, chopped</span></li>
    <li style="text-align:justify"><span style="background-color:#ecf0f1">2-3 cloves garlic, minced</span></li>
    <li style="text-align:justify"><span style="background-color:#ecf0f1">2 jalapeño peppers, roasted, peeled, de-seeded, chopped</span></li>
    <li style="text-align:justify"><span style="background-color:#ecf0f1">4-5 roma tomatoes, roasted peeled, chopped</span></li>
    <li style="text-align:justify"><span style="background-color:#ecf0f1">1 15 oz can kidney beans, strained and washed</span></li>
    <li style="text-align:justify"><span style="background-color:#ecf0f1">2 tsp salt</span></li>
    <li style="text-align:justify"><span style="background-color:#ecf0f1">2 tsp black pepper</span></li>
    <li style="text-align:justify"><span style="background-color:#ecf0f1">2 tsp cumin</span></li>
    <li style="text-align:justify"><span style="background-color:#ecf0f1">¼ - ½ tsp cayenne pepper</span></li>
    <li style="text-align:justify"><span style="background-color:#ecf0f1">1 tsp garlic powder</span></li>
    <li style="text-align:justify"><span style="background-color:#ecf0f1">1 tsp Mexican oregano</span></li>
    <li style="text-align:justify"><span style="background-color:#ecf0f1">1 tsp paprika</span></li>
    <li style="text-align:justify"><span style="background-color:#ecf0f1">1 tsp smoked paprika</span></li>
    <li style="text-align:justify"><span style="background-color:#ecf0f1">3 cups chicken broth</span></li>
    <li style="text-align:justify"><span style="background-color:#ecf0f1">2 tbsp tomato paste</span></li>
</ul>

<p style="text-align:justify"><strong>INSTRUCTIONS</strong></p>

<ol>
    <li style="text-align:justify">Heat a large put or Dutch oven over medium-high heat and brown the beef, while stirring to break it up. Cook until no longer pink. Drain out the liquid.</li>
    <li style="text-align:justify">Stir in onions and cook for about 5 minutes until they are pale and soft. Add in minced garlic and jalapeño peppers, stirring for another minute.</li>
    <li style="text-align:justify">Stir in the chopped tomatoes, all the spices, and tomato paste until well-distributed and tomato paste has broken up, then follow with the broth. Allow the pot to come to a gentle boil over medium heat, uncovered for about 20 minutes.</li>
    <li style="text-align:justify">Reduce heat to low, cover and simmer for at least 3 hours, until liquid has reduced.</li>
    <li style="text-align:justify">During the last 20-30 minutes of cook time, add in the kidney beans; uncover and allow liquid to reduce further during this time.</li>
    <
    li style="text-align:justify">Serve hot with jalapeño cornbread muffins, shredded cheese, avocado chunks, chopped cilantro, chopped green onion, tortilla chips.</li>
</ol>
Snerd
  • 1,463
  • 3
  • 25
  • 36
  • 3
    `break` is absolutely fine here. You don't need parentheses around `html` (just a stylistic issue) and have `file` not closed (`with open(...) as file: html = file.readlines()` should be preferred - this issue is much worse). `break` in a `for` loop is efficient and (IMO) most readable here. You should create `stripped` and `quoted` only on demand, though: you use them only in first `if` branch and just waste for non-`li` lines. – STerliakov Dec 12 '22 at 01:29
  • theoretically, you can replace `break` with something like `done=False; while not done.... if something: done = True`, but that would make little sense. – gog Dec 12 '22 at 01:36
  • @SUTerliakov nice suggestion on the stripped and quoted part..I fixed the parentheses too around html.. I don't get what you mean on the file .readlines() though.. – Snerd Dec 12 '22 at 01:37
  • @gog is a ``while`` even possible?.. I want the challenge. – Snerd Dec 12 '22 at 01:38
  • You should provide an example of parse.txt – areop-enap Dec 12 '22 at 01:44
  • `if "
  • " in line` is not doing what you think. See https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-for-equality-against-a-single-value
  • – John Gordon Dec 12 '22 at 01:53