所以我使用 python 来帮助将文本文件更改为 xml。下面的一些文本文件。
:
Breakfast Breads
Gluten Free Nut Loaf
Makes 1 loaf
150 g (51/2 oz./11/2 cups)
almond meal
30 g (1 oz./1/4 cup)
walnuts, coarsely chopped
1/4 teaspoon
gluten-free baking powder
我不确定这是否可以复制,因为它可能是文件本身的幽灵。但如果你能尝试那就太好了。
为了解释当前的步骤,我的代码很简陋,寻找冒号,它知道下一行是一般类别,然后切换一个步骤变量。然后下一步将跳过空行 '\r\n' 直到找到一个非空行,该行标识了单个配方的开头及其名称。循环中的下一行,如果为空,则更改步骤,否则为服务大小编写 xml,然后更改步骤。
在这里,我们到达成分步骤。所以奇数行是数量,偶数行是实际成分。
if step == 2:
if line == empty:
continue
else:
xmlMenu.write("\n\t<ingredients>")
qty = line
qtyXML = ("\n\t\t<qty>\n\t\t\t" + qty + "\t\t</qty>")
xmlMenu.write(qtyXML)
step = 2.2
continue
if step == 2.2:
if menu.next() == empty:
ingredient = line
pass
ingredientXML = ("\n\t\t<ingredient>\n\t\t\t" + ingredient + "\t\t</ingredient>\n\t</ingredients>")
xmlMenu.write(ingredientXML)
step = 3
continue
else:
ingredient = line
ingredientXML = ("\n\t\t<ingredient>\n\t\t\t" + ingredient + "\t\t</ingredient>\n\t</ingredients>")
xmlMenu.write(ingredientXML)
step = 2
continue
我的问题,当它达到第二组数量/成分时,它似乎跳过了杏仁粉的数量,因此搞砸了整个 xml 文件。
<recipe>
<category>
Breakfast Breads
</category>
<name>
Gluten Free Nut Loaf
</name>
<servings>
Makes 1 loaf
</servings>
<ingredients>
<qty>
150 g (51/2 oz./11/2 cups)
</qty>
<ingredient>
almond meal
</ingredient>
</ingredients>
<ingredients>
<qty>
walnuts, coarsely chopped
</qty>
<ingredient>
1/4 teaspoon
</ingredient>
</ingredients>
如果我的代码在某处只是废话,有人有什么想法吗?此外,任何有关更好方法的建议。我仍然是初学者级别的编程。
//编辑更多代码。当它开始一个新的配方时,代码在集合结束时仍在工作中。
empty = '\r\n'
colon = ':'
new_contents = []
category = ''
categoryXML = ''
recipe = ''
recipeXML = ''
servings = ''
servingsXML = ''
ingrediants = ''
ingrediantsXML = ''
ingredient = ''
ingredientXML = ''
qty = ''
qtyXML = ''
instructions = ''
instructionsXML = ''
intNo = ''
intNoXML = ''
instruction = ''
instructionXML = ''
step = ''
x = 0
menu = open("menuTextFormat.txt", 'r')
xmlMenu = open('menuTextXML.txt', 'w')
for line in menu:
# if not line.strip():
# continue
#else:
# new_contents.append(line)
if colon in line:
step = 0
continue
if step == 0:
if not line:
continue
else:
category = line
categoryXML = "<recipe>\n\t<category>\n\t\t" + category + "\t</category>"
xmlMenu.write(categoryXML)
step = 1
continue
if step == 1:
if line == empty:
continue
else:
recipe = line
recipeXML = ("\n\t<name>\n\t\t" + recipe + "\t</name>")
xmlMenu.write(recipeXML)
step = 12
continue
if step == 12:
if line == empty:
step = 2
continue
else:
servings = line
servingsXML = ("\n\t<servings>\n\t\t" + servings + "\t</servings>")
xmlMenu.write(servingsXML)
step = 2
continue
if step == 2:
if line == empty:
continue
else:
xmlMenu.write("\n\t<ingredients>")
qty = line
qtyXML = ("\n\t\t<qty>\n\t\t\t" + qty + "\t\t</qty>")
xmlMenu.write(qtyXML)
step = 22
continue
if step == 22:
if menu.next() == empty:
ingredient = line
ingredientXML = ("\n\t\t<ingredient>\n\t\t\t" + ingredient + "\t\t</ingredient>\n\t</ingredients>")
xmlMenu.write(ingredientXML)
step = 3
continue
else:
ingredient = line
ingredientXML = ("\n\t\t<ingredient>\n\t\t\t" + ingredient + "\t\t</ingredient>\n\t</ingredients>")
xmlMenu.write(ingredientXML)
step = 2
continue
if step == 3:
if line == empty:
continue
else:
x += 1
intNoXML = ("\n\t\t<id>\n\t\t\t" + str(x) + "\n\t\t</id>")
xmlMenu.write(intNoXML)
instruction = line
instructionXML = ("\n\t\t<instruction>\n\t\t\t" + instruction + "\t\t</instruction>")
xmlMenu.write(instructionXML)
if menu.next() == empty:
step = 4
continue
else:
continue
continue
if step == 4:
if not menu.next():
break
else:
step = 1
continue
#print line
#xmlMenu.write("".join(new_contents))
xmlMenu.close()
menu.close()
:
Breakfast Breads
Gluten Free Nut Loaf
Makes 1 loaf
150 g (51/2 oz./11/2 cups)
almond meal
30 g (1 oz./1/4 cup)
walnuts, coarsely chopped
1/4 teaspoon
gluten-free baking powder
1 teaspoon
ground cinnamon
95 g (31/4 oz./3/4 cup)
arrowroot (tapioca) flour
1/2 teaspoon
sea salt
3
organic eggs
1/2 teaspoon
stevia powder
3 tablespoons
grape seed oil
2 tablespoons
coconut Milk
1 teaspoon
apple cider vinegar
Preheat the oven to 180∫C (350∫F/Gas 4).
Grease a 20 x 9 cm (8 x 31/2 inch) loaf (bar) tin.
Put the almond meal, walnuts, baking powder, cinnamon, arrowroot flour and salt in a large bowl and mix well with a wooden spoon.
Crack the eggs into a separate bowl and whisk using an electric mixer until pale and fluffy, about 11/2 minutes.
Add the stevia, grape seed oil, coconut milk and vinegar and mix gently.
Pour the mixture into the dry ingredients and stir to combine.
Spoon the mixture into the greased tin and bake for about 40 minutes, or until a skewer inserted in the centre of the loaf comes out clean.
Remove the bread from the oven and leave to cool in the tin for a few minutes, before turning out onto a wire rack to cool completely.
Enjoy the bread melt-in-the-mouth warm, or at room temperature with your favourite topping.
Basic Gluten Free Loaf
2Ω cups
blanched almond flour
Ω teaspoon
baking soda
1 tsp.
bicarbonate of soda
3
eggs beaten
1 tsp.
stevia powder
Ω teaspoon
apple cider vinegar
!!
Pinch sea salt
Preheat oven to 250 degrees Celsius
Grease a small loaf pan
Combine dry ingredients in a bowl
In a separate bowl place eggs and apple cider vinegar
Spoon the mixture into loaf pan
Bake for 50 mins until set
Cool on wire rack
Slice when cool
更多的数据。