Search code examples
pythondesign-patternsblock

How to read a block of pattern file?


I have some pattern files that I like to read just a few parts of them.

Exemple: text.txt

Recipe: 
'name' 
Ingredients:
Item 1
Item 2
Item N... 
How to prepare:
Item 1
Item 2
Item N... 

I'd like to get only the ingredients.


Solution

  • handle = open("test.txt", "r")
    lines = handle.readlines()
    lines = [line.strip() for line in lines]
    st_idx = lines.index("Ingredients:")
    ed_idx = lines.index("How to prepare:")
    Ingredients = lines[st_idx:ed_idx]