Note that there are some explanatory texts on larger screens.

plurals
  1. POPython, unknown skip when reading/write document
    text
    copied!<p>So i'm using python to help change a text file into xml. Somme of the text document below.</p> <pre><code>: 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 </code></pre> <p>I'm not sure if this can be replicated since it could be a ghost in the file itself. But if you could try that would be great.</p> <p>To explain the current steps, my code is rudimentary, looking for the colon, it knows the next line is a general category, then switches a step variable. Then next step will skip empty lines '\r\n' until it finds a not empty line which identified the start of a individual recipe, with its name. Next line in the loop, if empty change step, else write xml for serving size then change step. </p> <p>Here we reach ingredients step. So the odd lines are qty, and even lines are the actual ingredient. </p> <pre><code>if step == 2: if line == empty: continue else: xmlMenu.write("\n\t&lt;ingredients&gt;") qty = line qtyXML = ("\n\t\t&lt;qty&gt;\n\t\t\t" + qty + "\t\t&lt;/qty&gt;") xmlMenu.write(qtyXML) step = 2.2 continue if step == 2.2: if menu.next() == empty: ingredient = line pass ingredientXML = ("\n\t\t&lt;ingredient&gt;\n\t\t\t" + ingredient + "\t\t&lt;/ingredient&gt;\n\t&lt;/ingredients&gt;") xmlMenu.write(ingredientXML) step = 3 continue else: ingredient = line ingredientXML = ("\n\t\t&lt;ingredient&gt;\n\t\t\t" + ingredient + "\t\t&lt;/ingredient&gt;\n\t&lt;/ingredients&gt;") xmlMenu.write(ingredientXML) step = 2 continue </code></pre> <p>My problem, when it reaches the second set of qty/ingredient, it seemingly skips the qty for the almond meal and so screws up the whole xml file. </p> <pre><code>&lt;recipe&gt; &lt;category&gt; Breakfast Breads &lt;/category&gt; &lt;name&gt; Gluten Free Nut Loaf &lt;/name&gt; &lt;servings&gt; Makes 1 loaf &lt;/servings&gt; &lt;ingredients&gt; &lt;qty&gt; 150 g (51/2 oz./11/2 cups) &lt;/qty&gt; &lt;ingredient&gt; almond meal &lt;/ingredient&gt; &lt;/ingredients&gt; &lt;ingredients&gt; &lt;qty&gt; walnuts, coarsely chopped &lt;/qty&gt; &lt;ingredient&gt; 1/4 teaspoon &lt;/ingredient&gt; &lt;/ingredients&gt; </code></pre> <p>Anyone have any thoughts if my code is just crap somewhere? Also, any suggestions for a better method to do this. I'm still beginner level programming.</p> <p>//Edit for more code. The code still in the works at the end of the set when it starts a new recipe.</p> <pre><code>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 = "&lt;recipe&gt;\n\t&lt;category&gt;\n\t\t" + category + "\t&lt;/category&gt;" xmlMenu.write(categoryXML) step = 1 continue if step == 1: if line == empty: continue else: recipe = line recipeXML = ("\n\t&lt;name&gt;\n\t\t" + recipe + "\t&lt;/name&gt;") xmlMenu.write(recipeXML) step = 12 continue if step == 12: if line == empty: step = 2 continue else: servings = line servingsXML = ("\n\t&lt;servings&gt;\n\t\t" + servings + "\t&lt;/servings&gt;") xmlMenu.write(servingsXML) step = 2 continue if step == 2: if line == empty: continue else: xmlMenu.write("\n\t&lt;ingredients&gt;") qty = line qtyXML = ("\n\t\t&lt;qty&gt;\n\t\t\t" + qty + "\t\t&lt;/qty&gt;") xmlMenu.write(qtyXML) step = 22 continue if step == 22: if menu.next() == empty: ingredient = line ingredientXML = ("\n\t\t&lt;ingredient&gt;\n\t\t\t" + ingredient + "\t\t&lt;/ingredient&gt;\n\t&lt;/ingredients&gt;") xmlMenu.write(ingredientXML) step = 3 continue else: ingredient = line ingredientXML = ("\n\t\t&lt;ingredient&gt;\n\t\t\t" + ingredient + "\t\t&lt;/ingredient&gt;\n\t&lt;/ingredients&gt;") xmlMenu.write(ingredientXML) step = 2 continue if step == 3: if line == empty: continue else: x += 1 intNoXML = ("\n\t\t&lt;id&gt;\n\t\t\t" + str(x) + "\n\t\t&lt;/id&gt;") xmlMenu.write(intNoXML) instruction = line instructionXML = ("\n\t\t&lt;instruction&gt;\n\t\t\t" + instruction + "\t\t&lt;/instruction&gt;") 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 </code></pre> <p>more of the data.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload