Depending on the content's name, converting a text file into a list of dictionaries or a list
Question:
I have a question, I’m not sure where to begin. I have a text file with all the contents of recipes and other things. I wonder if there’s a straightforward way to replace the text file and make it into the example of the function that I’ve pasted at the bottom. If not, does that imply that in order to turn some texts into lists and dictionaries by iterating through each line and splitting at certain places?
Text file:
name:Chocolate Chip Cookies
categories:dessert;cookie;chocolate
ingredient:2¼ cups all-purpose flour
ingredient:1 teaspoon baking soda
ingredient:1 teaspoon salts
ingredient:½ cup (1 stick) butter, softened
ingredient:¾ cup granulated sugar
ingredient:¾ cup packed brown sugar
ingredient:1 teaspoon vanilla extract
ingredient:2 large eggs
ingredient:2 cups (12-ounce package) Semi-Sweet Chocolate Chips
step:Preheat oven to 375°F
step:Combine flour, baking soda, and salt in a small bowl.
step:Beat butter, sugar, brown sugar, and vanilla extract in a large bowl until creamy.
step:Add eggs, beating well after each addition.
step:Gradually beat the flour mixture into the wet mixture.
step:Stir in morsels.
step:Drop rounded tablespoons of dough onto ungreased baking sheets.
step:Bake for 9 to 11 minutes or until golden brown.
step:Cool on baking sheets for 2 minutes.
step:Move to wire racks to cool completely.
name:Sugar Cookies
categories:dessert;cookie;kid's favorites
ingredient:2¾ cups all-purpose flour
ingredient:1 teaspoon baking soda
ingredient:½ teaspoon baking powder
ingredient:1 cup butter, softened
ingredient:1½ cups white sugar
ingredient:1 egg
ingredient:1 teaspoon vanilla extract
step:Preheat oven to 375°F
step:In a small bowl, stir together flour, baking soda, and baking powder. Set aside.
step:In a large bowl, mix together butter and sugar until smooth. Beat in egg and vanilla.
step:Blend the wet and dry ingredients.
step:Place teaspoon-size balls of dough onto an ungreased cookie sheet.
step:Bake 8 to 10 minutes in the preheated oven, or until golden.
step:Let stand on cookie sheet two minutes before removing to cool on wire racks.
def load_data():
return [
{
'name': 'Chocolate Chip Cookies',
'categories': ['dessert', 'cookie', 'chocolate'],
'ingredients': ['2¼ cups all-purpose flour',
'1 teaspoon baking soda',
'1 teaspoon salts',
'½ cup (1 stick) butter, softened',
'¾ cup granulated sugar',
'¾ cup packed brown sugar',
'1 teaspoon vanilla extract',
'2 large eggs',
'2 cups (12-ounce package) Semi-Sweet Chocolate Chips'],
'steps': ['Preheat oven to 375°F',
'Combine flour, baking soda, and salt in a small bowl.',
'Beat butter, sugar, brown sugar, and vanilla extract in a large bowl until creamy.',
'Add eggs, beating well after each addition.',
'Gradually beat the flour mixture into the wet mixture.',
'Stir in morsels.',
'Drop rounded tablespoons of dough onto ungreased baking sheets.',
'Bake for 9 to 11 minutes or until golden brown.',
'Cool on baking sheets for 2 minutes.',
'Move to wire racks to cool completely.']
},
{
'name': 'Sugar Cookies',
'categories': ['dessert', 'cookie'],
'ingredients': ['2¾ cups all-purpose flour',
'1 teaspoon baking soda',
'½ teaspoon baking powder',
'1 cup butter, softened',
'1½ cups white sugar',
'1 egg',
'1 teaspoon vanilla extract'],
'steps': ['Preheat oven to 375°F',
'In a small bowl, stir together flour, baking soda, and baking powder. Set aside.',
'In a large bowl, mix together butter and sugar until smooth. Beat in egg and vanilla.',
'Blend the wet and dry ingredients.',
'Place teaspoon-size balls of dough onto an ungreased cookie sheet.',
'Bake 8 to 10 minutes in the preheated oven, or until golden.',
'Let stand on cookie sheet two minutes before removing to cool on wire racks.']
}
]
Answers:
To make a list of dictionaries from the text you can do (text
variable contains string from your question):
out, current = [], None
for line in text.splitlines():
if line.startswith("name:"):
if current:
out.append(current)
current = {"name": line.split(":", maxsplit=1)[1]}
elif ":" in line:
k, v = line.split(":", maxsplit=1)
if k == "categories":
v = v.split(";")
current.setdefault(k, []).extend(v)
else:
current.setdefault(k, []).append(v)
if current:
out.append(current)
print(out)
Prints:
[
{
"name": "Chocolate Chip Cookies",
"categories": ["dessert", "cookie", "chocolate"],
"ingredient": [
"2¼ cups all-purpose flour",
"1 teaspoon baking soda",
"1 teaspoon salts",
"½ cup (1 stick) butter, softened",
"¾ cup granulated sugar",
"¾ cup packed brown sugar",
"1 teaspoon vanilla extract",
"2 large eggs",
"2 cups (12-ounce package) Semi-Sweet Chocolate Chips",
],
"step": [
"Preheat oven to 375°F",
"Combine flour, baking soda, and salt in a small bowl.",
"Beat butter, sugar, brown sugar, and vanilla extract in a large bowl until creamy.",
"Add eggs, beating well after each addition.",
"Gradually beat the flour mixture into the wet mixture.",
"Stir in morsels.",
"Drop rounded tablespoons of dough onto ungreased baking sheets.",
"Bake for 9 to 11 minutes or until golden brown.",
"Cool on baking sheets for 2 minutes.",
"Move to wire racks to cool completely.",
],
},
{
"name": "Sugar Cookies",
"categories": ["dessert", "cookie", "kid's favorites"],
"ingredient": [
"2¾ cups all-purpose flour",
"1 teaspoon baking soda",
"½ teaspoon baking powder",
"1 cup butter, softened",
"1½ cups white sugar",
"1 egg",
"1 teaspoon vanilla extract ",
],
"step": [
"Preheat oven to 375°F",
"In a small bowl, stir together flour, baking soda, and baking powder. Set aside.",
"In a large bowl, mix together butter and sugar until smooth. Beat in egg and vanilla.",
"Blend the wet and dry ingredients.",
"Place teaspoon-size balls of dough onto an ungreased cookie sheet.",
"Bake 8 to 10 minutes in the preheated oven, or until golden.",
"Let stand on cookie sheet two minutes before removing to cool on wire racks.",
],
},
]
I have a question, I’m not sure where to begin. I have a text file with all the contents of recipes and other things. I wonder if there’s a straightforward way to replace the text file and make it into the example of the function that I’ve pasted at the bottom. If not, does that imply that in order to turn some texts into lists and dictionaries by iterating through each line and splitting at certain places?
Text file:
name:Chocolate Chip Cookies
categories:dessert;cookie;chocolate
ingredient:2¼ cups all-purpose flour
ingredient:1 teaspoon baking soda
ingredient:1 teaspoon salts
ingredient:½ cup (1 stick) butter, softened
ingredient:¾ cup granulated sugar
ingredient:¾ cup packed brown sugar
ingredient:1 teaspoon vanilla extract
ingredient:2 large eggs
ingredient:2 cups (12-ounce package) Semi-Sweet Chocolate Chips
step:Preheat oven to 375°F
step:Combine flour, baking soda, and salt in a small bowl.
step:Beat butter, sugar, brown sugar, and vanilla extract in a large bowl until creamy.
step:Add eggs, beating well after each addition.
step:Gradually beat the flour mixture into the wet mixture.
step:Stir in morsels.
step:Drop rounded tablespoons of dough onto ungreased baking sheets.
step:Bake for 9 to 11 minutes or until golden brown.
step:Cool on baking sheets for 2 minutes.
step:Move to wire racks to cool completely.
name:Sugar Cookies
categories:dessert;cookie;kid's favorites
ingredient:2¾ cups all-purpose flour
ingredient:1 teaspoon baking soda
ingredient:½ teaspoon baking powder
ingredient:1 cup butter, softened
ingredient:1½ cups white sugar
ingredient:1 egg
ingredient:1 teaspoon vanilla extract
step:Preheat oven to 375°F
step:In a small bowl, stir together flour, baking soda, and baking powder. Set aside.
step:In a large bowl, mix together butter and sugar until smooth. Beat in egg and vanilla.
step:Blend the wet and dry ingredients.
step:Place teaspoon-size balls of dough onto an ungreased cookie sheet.
step:Bake 8 to 10 minutes in the preheated oven, or until golden.
step:Let stand on cookie sheet two minutes before removing to cool on wire racks.
def load_data():
return [
{
'name': 'Chocolate Chip Cookies',
'categories': ['dessert', 'cookie', 'chocolate'],
'ingredients': ['2¼ cups all-purpose flour',
'1 teaspoon baking soda',
'1 teaspoon salts',
'½ cup (1 stick) butter, softened',
'¾ cup granulated sugar',
'¾ cup packed brown sugar',
'1 teaspoon vanilla extract',
'2 large eggs',
'2 cups (12-ounce package) Semi-Sweet Chocolate Chips'],
'steps': ['Preheat oven to 375°F',
'Combine flour, baking soda, and salt in a small bowl.',
'Beat butter, sugar, brown sugar, and vanilla extract in a large bowl until creamy.',
'Add eggs, beating well after each addition.',
'Gradually beat the flour mixture into the wet mixture.',
'Stir in morsels.',
'Drop rounded tablespoons of dough onto ungreased baking sheets.',
'Bake for 9 to 11 minutes or until golden brown.',
'Cool on baking sheets for 2 minutes.',
'Move to wire racks to cool completely.']
},
{
'name': 'Sugar Cookies',
'categories': ['dessert', 'cookie'],
'ingredients': ['2¾ cups all-purpose flour',
'1 teaspoon baking soda',
'½ teaspoon baking powder',
'1 cup butter, softened',
'1½ cups white sugar',
'1 egg',
'1 teaspoon vanilla extract'],
'steps': ['Preheat oven to 375°F',
'In a small bowl, stir together flour, baking soda, and baking powder. Set aside.',
'In a large bowl, mix together butter and sugar until smooth. Beat in egg and vanilla.',
'Blend the wet and dry ingredients.',
'Place teaspoon-size balls of dough onto an ungreased cookie sheet.',
'Bake 8 to 10 minutes in the preheated oven, or until golden.',
'Let stand on cookie sheet two minutes before removing to cool on wire racks.']
}
]
To make a list of dictionaries from the text you can do (text
variable contains string from your question):
out, current = [], None
for line in text.splitlines():
if line.startswith("name:"):
if current:
out.append(current)
current = {"name": line.split(":", maxsplit=1)[1]}
elif ":" in line:
k, v = line.split(":", maxsplit=1)
if k == "categories":
v = v.split(";")
current.setdefault(k, []).extend(v)
else:
current.setdefault(k, []).append(v)
if current:
out.append(current)
print(out)
Prints:
[
{
"name": "Chocolate Chip Cookies",
"categories": ["dessert", "cookie", "chocolate"],
"ingredient": [
"2¼ cups all-purpose flour",
"1 teaspoon baking soda",
"1 teaspoon salts",
"½ cup (1 stick) butter, softened",
"¾ cup granulated sugar",
"¾ cup packed brown sugar",
"1 teaspoon vanilla extract",
"2 large eggs",
"2 cups (12-ounce package) Semi-Sweet Chocolate Chips",
],
"step": [
"Preheat oven to 375°F",
"Combine flour, baking soda, and salt in a small bowl.",
"Beat butter, sugar, brown sugar, and vanilla extract in a large bowl until creamy.",
"Add eggs, beating well after each addition.",
"Gradually beat the flour mixture into the wet mixture.",
"Stir in morsels.",
"Drop rounded tablespoons of dough onto ungreased baking sheets.",
"Bake for 9 to 11 minutes or until golden brown.",
"Cool on baking sheets for 2 minutes.",
"Move to wire racks to cool completely.",
],
},
{
"name": "Sugar Cookies",
"categories": ["dessert", "cookie", "kid's favorites"],
"ingredient": [
"2¾ cups all-purpose flour",
"1 teaspoon baking soda",
"½ teaspoon baking powder",
"1 cup butter, softened",
"1½ cups white sugar",
"1 egg",
"1 teaspoon vanilla extract ",
],
"step": [
"Preheat oven to 375°F",
"In a small bowl, stir together flour, baking soda, and baking powder. Set aside.",
"In a large bowl, mix together butter and sugar until smooth. Beat in egg and vanilla.",
"Blend the wet and dry ingredients.",
"Place teaspoon-size balls of dough onto an ungreased cookie sheet.",
"Bake 8 to 10 minutes in the preheated oven, or until golden.",
"Let stand on cookie sheet two minutes before removing to cool on wire racks.",
],
},
]