Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python XML-创建n个子元素_Python_Xml - Fatal编程技术网

Python XML-创建n个子元素

Python XML-创建n个子元素,python,xml,Python,Xml,如何创建n个子元素?元素数以前是由给定值(计数)定义的。所有元素的元素名称及其值都相同。 例如: <root> <selem>-9999</selem> <selem>-9999</selem> <selem>-9999</selem> <selem>-9999</selem> . .

如何创建n个子元素?元素数以前是由给定值(计数)定义的。所有元素的元素名称及其值都相同。 例如:

 <root>
      <selem>-9999</selem>
      <selem>-9999</selem>
      <selem>-9999</selem>
      <selem>-9999</selem>
              .
              .
              .
              n
 </root>

Tnx

不能将变量赋值之类的语句放入列表中。只需使用普通for循环:

for i in xrange(count):
    elem = etree.SubElement(top, 'selem')
    elem.text = no_data
    top.append(elem)

不能将变量赋值之类的语句放入列表中。只需使用普通for循环:

for i in xrange(count):
    elem = etree.SubElement(top, 'selem')
    elem.text = no_data
    top.append(elem)

SubElement
自动将元素添加到其父元素中,因此无需显式附加/扩展它们。因此,无需使用列表理解,只需使用for循环:

with open('file_count.xml','r') as file_count:
    data = etree.prase(file_count)
count = len(data.findall('.//timePosition'))

no_data = '-9999'
top = etree.Element('root')
for i in xrange(count):
    etree.SubElement(top, 'selem').text = no_data
 no_data_xml = etree.ElementTree(top)
 no_data_xml.write(new.xml')

SubElement
自动将元素添加到其父元素中,因此无需显式附加/扩展它们。因此,无需使用列表理解,只需使用for循环:

with open('file_count.xml','r') as file_count:
    data = etree.prase(file_count)
count = len(data.findall('.//timePosition'))

no_data = '-9999'
top = etree.Element('root')
for i in xrange(count):
    etree.SubElement(top, 'selem').text = no_data
 no_data_xml = etree.ElementTree(top)
 no_data_xml.write(new.xml')
谢谢仅编辑prase-->解析;对于xrange中的i(计数)-->对于xrange中的i(计数):谢谢。仅编辑prase-->解析;对于X范围内的i(计数)-->对于X范围内的i(计数):