Python: lxml formatting
Question: I need to create an xml file. Is there a possibility to get the following
formatting? The len of the list is normally bigger and always different. So I
can not use if loop with a request of the list length.

Needed formatting:

    
    
    <test>
      <fanart>
        <thumb preview="http://image.tmdb.org/t/p/w342/krkkgbtWHlMXVLbPGdIxzxKJERM.jpg">http://image.tmdb.org/t/p/original/krkkgbtWHlMXVLbPGdIxzxKJERM.jpg</thumb>
        <thumb preview="http://image.tmdb.org/t/p/w342/izYCpovyAIKLI2i3gmhSKxlR8Pk.jpg">http://image.tmdb.org/t/p/original/izYCpovyAIKLI2i3gmhSKxlR8Pk.jpg</thumb>
        <thumb preview="http://image.tmdb.org/t/p/w342/vmrnxaYx1xlG5jhuFUs51dd3VPA.jpg">http://image.tmdb.org/t/p/original/vmrnxaYx1xlG5jhuFUs51dd3VPA.jpg</thumb>
        <thumb preview="http://image.tmdb.org/t/p/w342/pPFbXcONHBntJIAsEn8TaIPPCpZ.jpg">http://image.tmdb.org/t/p/original/pPFbXcONHBntJIAsEn8TaIPPCpZ.jpg</thumb>
      </fanart>
    </test>
    

Code:

    
    
    import lxml.builder
    
    E = lxml.builder.ElementMaker()
    nfo = E.test()
    
    list = [('http://image.tmdb.org/t/p/w342/krkkgbtWHlMXVLbPGdIxzxKJERM.jpg','http://image.tmdb.org/t/p/original/krkkgbtWHlMXVLbPGdIxzxKJERM.jpg'),
            ('http://image.tmdb.org/t/p/w342/izYCpovyAIKLI2i3gmhSKxlR8Pk.jpg','http://image.tmdb.org/t/p/original/izYCpovyAIKLI2i3gmhSKxlR8Pk.jpg'),
            ('http://image.tmdb.org/t/p/w342/vmrnxaYx1xlG5jhuFUs51dd3VPA.jpg','http://image.tmdb.org/t/p/original/vmrnxaYx1xlG5jhuFUs51dd3VPA.jpg'),
            ('http://image.tmdb.org/t/p/w342/pPFbXcONHBntJIAsEn8TaIPPCpZ.jpg','http://image.tmdb.org/t/p/original/pPFbXcONHBntJIAsEn8TaIPPCpZ.jpg')]
    
    for (link_thumb, link_orig) in list:
        fanart = E.fanart(E.thumb(link_orig,preview=link_thumb))
        nfo.append(fanart)
    
    
    print lxml.etree.tostring(nfo, pretty_print=True)
    

Output:

    
    
    <test>
      <fanart>
        <thumb preview="http://image.tmdb.org/t/p/w342/krkkgbtWHlMXVLbPGdIxzxKJERM.jpg">http://image.tmdb.org/t/p/original/krkkgbtWHlMXVLbPGdIxzxKJERM.jpg</thumb>
      </fanart>
      <fanart>
        <thumb preview="http://image.tmdb.org/t/p/w342/izYCpovyAIKLI2i3gmhSKxlR8Pk.jpg">http://image.tmdb.org/t/p/original/izYCpovyAIKLI2i3gmhSKxlR8Pk.jpg</thumb>
      </fanart>
      <fanart>
        <thumb preview="http://image.tmdb.org/t/p/w342/vmrnxaYx1xlG5jhuFUs51dd3VPA.jpg">http://image.tmdb.org/t/p/original/vmrnxaYx1xlG5jhuFUs51dd3VPA.jpg</thumb>
      </fanart>
      <fanart>
        <thumb preview="http://image.tmdb.org/t/p/w342/pPFbXcONHBntJIAsEn8TaIPPCpZ.jpg">http://image.tmdb.org/t/p/original/pPFbXcONHBntJIAsEn8TaIPPCpZ.jpg</thumb>
      </fanart>
    </test>
    

Thank you!


Answer: You appended `fanart` elements for each `image`; instead make 1 `fanart`
element and append it to the `nfo`; then append each image into that `fanart`
element:

    
    
    import lxml.builder
    
    E = lxml.builder.ElementMaker()
    nfo = E.test()
    fanart = E.fanart()
    nfo.append(fanart)    
    
    items = [('http://image.tmdb.org/t/p/w342/krkkgbtWHlMXVLbPGdIxzxKJERM.jpg','http://image.tmdb.org/t/p/original/krkkgbtWHlMXVLbPGdIxzxKJERM.jpg'),
            ('http://image.tmdb.org/t/p/w342/izYCpovyAIKLI2i3gmhSKxlR8Pk.jpg','http://image.tmdb.org/t/p/original/izYCpovyAIKLI2i3gmhSKxlR8Pk.jpg'),
            ('http://image.tmdb.org/t/p/w342/vmrnxaYx1xlG5jhuFUs51dd3VPA.jpg','http://image.tmdb.org/t/p/original/vmrnxaYx1xlG5jhuFUs51dd3VPA.jpg'),
            ('http://image.tmdb.org/t/p/w342/pPFbXcONHBntJIAsEn8TaIPPCpZ.jpg','http://image.tmdb.org/t/p/original/pPFbXcONHBntJIAsEn8TaIPPCpZ.jpg')]
    
    for (link_thumb, link_orig) in items:
        fanart.append(E.thumb(link_orig,preview=link_thumb))    
    
    print lxml.etree.tostring(nfo, pretty_print=True)
    

