1

我正在尝试创建一个应用程序来创建一个 xml 文件,并且我想为某些元素分配一个文本。此文本包含文件夹上的图像文件。代码如下:

    import glob
    import os
    import os.path

    from xml.etree import ElementTree
    from xml.dom import minidom
    import xml.etree.ElementTree as ET

    def prettify(elem):
    """Return a pretty-printed XML string for the Element.
    """
        rough_string = ElementTree.tostring(elem, 'utf-8')
        reparsed = minidom.parseString(rough_string)
        return reparsed.toprettyxml(indent="  ")

    path = "/home/unkuiri/Ubuntu One/Wallpapers/*"


    background = ET.Element('background')
    starttime = ET.SubElement(background, 'starttime')
    year = ET.SubElement(starttime, 'year')
    month = ET.SubElement(starttime, 'month')
    day = ET.SubElement(starttime, 'day')
    hour = ET.SubElement(starttime, 'hour')
    minute = ET.SubElement(starttime, 'minute')
    second = ET.SubElement(starttime, 'second')
    static = ET.SubElement(background, 'static')
    duration_stat = ET.SubElement(static, 'duration')
    files = ET.SubElement(static, 'file')
    transition = ET.SubElement(background, 'transition')
    duration_trans = ET.SubElement(transition, 'duration')
    from1 = ET.SubElement(transition, 'from')
    to = ET.SubElement(transition, 'to')

    dirList = glob.glob(path)

    while len(background.findall("./static/file")) <= len([name for name in os.listdir('.') if os.path.isfile(name)]):
            background.append(static)
            background.append(transition)
            continue

    for fname in dirList:   

        to.text = fname
        files.text = fname
        from1.text = fname


    print prettify(background)

此代码输出格式正确的 xml,但仅使用最后一个路径,重复次数与文件夹中的文件数一样多。我想要的是它为每个“文件”元素打印一个路径,并在前面的“to”元素和下一个“元素”上打印相同的路径。也许这是一个我不知道的简单解决方案。我还是个新手。

提前致谢

4

3 回答 3

3

您只是创建一个元素并多次添加相同的元素。

fname在你的 for 循环中,你一遍又一遍地分配该元素的成员,所以最后它只剩下最后一个

您需要创建一个新元素并每次在 for 循环中填充它

可能你应该有更多这样的东西

background = ET.Element('background')
dirList = glob.glob(path)

for fname in dirList:   

    starttime = ET.SubElement(background, 'starttime')
    year = ET.SubElement(starttime, 'year')
    month = ET.SubElement(starttime, 'month')
    day = ET.SubElement(starttime, 'day')
    hour = ET.SubElement(starttime, 'hour')
    minute = ET.SubElement(starttime, 'minute')
    second = ET.SubElement(starttime, 'second')
    static = ET.SubElement(background, 'static')
    duration_stat = ET.SubElement(static, 'duration')
    files = ET.SubElement(static, 'file')
    transition = ET.SubElement(background, 'transition')
    duration_trans = ET.SubElement(transition, 'duration')
    from1 = ET.SubElement(transition, 'from')
    to = ET.SubElement(transition, 'to')

    to.text = fname
    files.text = fname
    from1.text = fname
于 2012-10-31T00:33:25.460 回答
0

我已经设法用这段代码解决了这个问题:

    import glob
    from xml.etree import ElementTree
    from xml.dom import minidom

    def prettify(elem):
        """Return a pretty-printed XML string for the Element.
        """
        rough_string = ElementTree.tostring(elem, 'utf-8')
        reparsed = minidom.parseString(rough_string)
        return reparsed.toprettyxml(indent="  ")

    import xml.etree.ElementTree as ET

    path = "/home/unkuiri/Ubuntu One/Wallpapers/*"


    background = ET.Element('background')
    dirList = glob.glob(path)
    starttime = ET.SubElement(background, 'starttime')
    year = ET.SubElement(starttime, 'year')
    year.text = '2012'
    month = ET.SubElement(starttime, 'month')
    month.text = '10'
    day = ET.SubElement(starttime, 'day')
    day.text = '10'
    hour = ET.SubElement(starttime, 'hour')
    hour.text = '00'
    minute = ET.SubElement(starttime, 'minute')
    minute.text = '00'
    second = ET.SubElement(starttime, 'second')
    second.text = '00'


    for i,fname in enumerate(dirList):    

        static = ET.SubElement(background, 'static')
        duration_stat = ET.SubElement(static, 'duration')
        duration_stat.text = '1795.0'
        files = ET.SubElement(static, 'file')
        transition = ET.SubElement(background, 'transition')
        duration_trans = ET.SubElement(transition, 'duration')
        duration_trans.text = '5.0'
        from1 = ET.SubElement(transition, 'from')
        to = ET.SubElement(transition, 'to')

        from1.text = dirList[i-1]

        files.text = dirList[i-1]

        to.text = dirList[i]


    print prettify(background)
于 2012-11-06T15:06:51.913 回答
0

您在每次迭代时都踩到 /files/from1 而不将它们保存在任何地方。在您有机会对数据进行任何操作之前,每次迭代都会覆盖上一次迭代的内容。

你的意思是把这些变量存储起来吗?你的意思是print prettify(background)每次迭代都做吗?

于 2012-10-31T00:30:23.290 回答