简体   繁体   English

Python - for循环仅输出最后一次迭代

[英]Python - for loop only outputs the last iteration

I'm trying to make an application to create a xml file and I want to assign a text to certain elements. 我正在尝试创建一个应用程序来创建一个xml文件,我想为某些元素分配一个文本。 This text consists on image files on a folder. 此文本包含文件夹上的图像文件。 The code is as follows: 代码如下:

    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)

This code outputs a correctly formatted xml but only with the last path, repeating it as many times as the number of files in folder. 此代码输出格式正确的xml,但只输出最后一个路径,重复次数与文件夹中的文件数一样多。 What I want is for it to print one path per "file" element and that same path on the preceeding "to" element and the next from "element". 我想要的是它为每个“file”元素打印一个路径,在前面的“to”元素上打印相同的路径,从“element”打印下一个路径。 Maybe it is a simple solution that I'm not aware of. 也许这是一个我不知道的简单解决方案。 I'm still a newbie. 我还是个新手。

Thanks in advance 提前致谢

You are just creating a single element and adding this same element multiple times. 您只需创建一个元素并多次添加此元素

In your for loop, you are assigning the members of that element over and over, so finally it ends up with just the last fname in there 在你的for循环中,你一遍又一遍地分配该元素的成员,所以最后它最终只有那里的最后一个fname

You need to create a fresh element and populate it each time in the for loop 您需要创建一个新元素并在每次for循环中填充它

Probably you should have something more like this 也许你应该有更像这样的东西

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

You are stomping over to/files/from1 on each iteration without saving them anywhere. 您在每次迭代时都会踩到/ files / from1而不将它们保存在任何地方。 Each iteration overwrites whats from the previous iteration before you have a chance to do anything with the data. 在您有机会对数据执行任何操作之前,每次迭代都会覆盖上一次迭代中的内容。

Did you mean to store these variables away? 你的意思是存储这些变量吗? Did you mean to do print prettify(background) on each iteration? 你是不是想在每次迭代中做print prettify(background)

I've managed to solve this question with this code: 我已经设法用这段代码解决了这个问题:

    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)

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

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