我需要创建一个xml文件。是否有可能获得以下格式?列表中的len通常更大并且总是不同的。所以我不能使用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>
谢谢!
答案 0 :(得分:2)
您为每个fanart
添加了image
个元素;而是生成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)