Python: lxml格式

Python: lxml formatting

我需要创建一个 xml 文件。是否有可能获得以下格式?列表的长度通常更大并且总是不同的。所以我不能对列表长度的请求使用 if 循环。

需要格式化:

<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>

代码:

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)

输出:

<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>

谢谢!

您为每个 image 添加了 fanart 个元素;而是制作 1 个 fanart 元素并将其附加到 nfo;然后将每个图像附加到 fanart 元素中:

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)