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>