简体   繁体   English

每次我在python中循环时如何创建节点

[英]How to create a nodes every time i go through the loop in python

I like to create a nodes every time when i go through a loop.But presently only the last value of the loop will be used. 我每次循环时都喜欢创建一个节点,但目前仅使用循环的最后一个值。 How can i achieve this using python. 我如何使用python实现此目的。 following is my example. 以下是我的示例。

My xml :- 我的XML:-

<person>
<user name="david" password="super"></user>
<user name="alen" password="boss"></user>
<user name="windeesal" password="sp"></user>
</person>

The python code: python代码:

import xml.etree.ElementTree as ET

doc = ET.parse("users.xml")
root = doc.getroot() #Returns the root element for this tree.
root.keys()          #Returns the elements attribute names as a list. The names are returned in an arbitrary order
for child in root:
    name = child.attrib['name']
    password = child.attrib['password']

root = ET.Element("person")
user = ET.SubElement(root, "user")
user.set("username",username)
user.set("password",password)

tree = ET.ElementTree(root)
myxml = tree.write("new.xml")

print myxml 

Out put of the code contain only last value of loop :( 代码的输出只包含loop :(

<person>
<user password="sp" username="windeesal" />
</person>

how to create the nodes every time i go through the loop then join the results and write them to the file .? 每次循环时如何创建节点,然后将结果加入并将其写入文件。 am really a beginner please give me a detail explanation. 我真的是一个初学者,请给我详细说明。 Thank you very much . 非常感谢你 。

Try next one. 尝试下一个。 Your understanding of python seems to be very basic so I am not sure what to write about problem. 您对python的理解似乎非常基础,所以我不确定该写些什么。

Please ask if you need explanation! 请询问您是否需要解释! :) :)

import xml.etree.ElementTree as ET

doc    = ET.parse("users.xml")
root = doc.getroot() #Returns the root element for this tree.
root_new  = ET.Element("person") 
for child in root:
    name                = child.attrib['name']
    password             = child.attrib['password']

    user  = ET.SubElement(root_new, "user") # create subelement in cycle! 
    user.set("username",name)               # username variable is not declared
    user.set("password",password)

tree = ET.ElementTree(root_new)
tree.write("new.xml")

import sys
tree.write(sys.stdout)

The trick to "creating nodes every time I go through a loop" is to create the node inside the loop. “每次通过循环创建节点”的技巧是在循环内部创建节点。 You want: 你要:

for child in root:
    name     = child.attrib['name']
    password = child.attrib['password']

    user = ET.SubElement(root_new, "user")
    user.set("username", name)
    user.set("password", password)

Python is whitespace-sensitive. Python对空格敏感。 If you don't indent those bottom three lines, they're not part of the loop. 如果您不缩进最后三行,则它们不属于循环的一部分。

You are overwriting the tree read in root. 您将覆盖从根读取的树。 Append to what youve read 附加到您阅读的内容

import xml.etree.ElementTree as ET

doc    = ET.parse("users.xml")
root = doc.getroot() #Returns the root element for this tree.
root.keys()          #Returns the elements attribute names as a list. The names are returned in an arbitrary order
for child in root:
    name                = child.attrib['name']
    password             = child.attrib['password']

user  = ET.SubElement(root, "user")
user.set("username",'test')
user.set("password",'me')

tree = ET.ElementTree(root)
tree.write("new.xml")

Also check new.xml for result. 还要检查new.xml的结果。 tree.write returns None apparently tree.write显然没有返回

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM