请原谅我,因为我是编码的新手。我有一个xml文件,其中包含某种格式的食谱(如下)。我想浏览标题,然后选择在创建新XML之前删除“食谱”还是保留它。如果我在第一个记录上按“ a”,它将删除该第一个记录,但没有为我提供第二个选项。如果我没有在第一条记录上按“ a”,第二条记录就会出现,并且我可以选择删除它。
删除记录后,似乎正在跳过记录。希望这是有道理的。
XML格式:
<recipes>
<recipe>
<title> Chicken and apple sandwiches with poppy seed mayo </title>
<nutrition Cholesterol="12m" calories="132" carbohydrate="14.1" fat="2.3" fibre="1.5" protein="5.5" saturated_fat="1.5" sodium="187m" sugar="1.6"/>
<description>Finger sandwiches are an essential part of high tea. Each one should be about two fingers thick, so they can be eaten in two or three bites.</description>
<taxonomy name="category"><term> High tea recipes</term></taxonomy>
<servings quantity="24" unit="People"/><prepTime quantity="30" unit="minutes"/><cookTime quantity="" unit="minutes"/>
<ingredients>
<ingredient name="1/2 large green apple, finely chopped"/>
<ingredient name="1 tablespoon lemon juice"/>
<ingredient name="2 1/2 cups chopped barbecue chicken breast"/>
<ingredient name="2 celery sticks, finely chopped"/>
<ingredient name="2 1/2 tablespoons chopped fresh chives"/>
<ingredient name="125g (1/2 cup) whole egg mayonnaise"/>
<ingredient name="1 tablespoon poppy seeds"/>
<ingredient name="1 1/2 teaspoons Dijon mustard"/>
<ingredient name="16 slices soft light rye bread"/>
<ingredient name="2 tablespoons butter, at room temperature"/>
<ingredient name="Baby herbs, to decorate (optional)"/>
</ingredients>
<instructions>
<instruction> Combine the apple and lemon juice in a bowl. Toss to coat. Add the chicken, celery, chives, mayonnaise, poppy seeds and mustard. Season. Mix until well combined. </instruction>
<instruction> Arrange half the bread slices on a work surface. Spread with the butter. Spread with the chicken mixture. Sandwich together with remaining bread slices. Use a serrated knife to remove the crusts from each sandwich.Cut each sandwich lengthways into thirds and decorate with baby herbs, if using. </instruction>
</instructions>
</recipe>
<recipe>
<title> Title 2 </title>
<nutrition Cholesterol="12m" calories="132" carbohydrate="14.1" fat="2.3" fibre="1.5" protein="5.5" saturated_fat="1.5" sodium="187m" sugar="1.6"/>
<description>Finger sandwiches are an essential part of high tea. Each one should be about two fingers thick, so they can be eaten in two or three bites.</description>
<taxonomy name="category"><term> High tea recipes</term></taxonomy>
<servings quantity="24" unit="People"/><prepTime quantity="30" unit="minutes"/><cookTime quantity="" unit="minutes"/>
<ingredients>
<ingredient name="1/2 large green apple, finely chopped"/>
<ingredient name="1 tablespoon lemon juice"/>
<ingredient name="2 1/2 cups chopped barbecue chicken breast"/>
<ingredient name="2 celery sticks, finely chopped"/>
<ingredient name="2 1/2 tablespoons chopped fresh chives"/>
<ingredient name="125g (1/2 cup) whole egg mayonnaise"/>
<ingredient name="1 tablespoon poppy seeds"/>
<ingredient name="1 1/2 teaspoons Dijon mustard"/>
<ingredient name="16 slices soft light rye bread"/>
<ingredient name="2 tablespoons butter, at room temperature"/>
<ingredient name="Baby herbs, to decorate (optional)"/>
</ingredients>
<instructions>
<instruction> Combine the apple and lemon juice in a bowl. Toss to coat. Add the chicken, celery, chives, mayonnaise, poppy seeds and mustard. Season. Mix until well combined. </instruction>
<instruction> Arrange half the bread slices on a work surface. Spread with the butter. Spread with the chicken mixture. Sandwich together with remaining bread slices. Use a serrated knife to remove the crusts from each sandwich.Cut each sandwich lengthways into thirds and decorate with baby herbs, if using. </instruction>
</instructions>
</recipe>
</recipes>
python代码:
import xml.etree.cElementTree as ET
tree = ET.ElementTree(file='test.xml')
root = tree.getroot()
for child in root:
for title in child:
if (title.tag=="title"):
print title.text
x=raw_input()
if (x=="a"):
root.remove(child)
print("deleted: "+ title.text)
tree.write('output.xml')
print "complete"
有人可以告诉我我做错了什么吗?预先感谢