Python ElementTree XMLWriter在文件末尾的奇怪行为
下面是我用来从MonoDB查询动态构建XML文件的代码:Python ElementTree XMLWriter在文件末尾的奇怪行为,python,elementtree,xmlwriter,Python,Elementtree,Xmlwriter,下面是我用来从MonoDB查询动态构建XML文件的代码: w = XMLWriter(filename,'utf-8') schedule = w.start('Schedule') for p in placements: ep = db.episodes.find_one({'HouseID':p['HouseID']}) if ep: w.start('Row') w.element('Time','%s' % p['Start'].strf
w = XMLWriter(filename,'utf-8')
schedule = w.start('Schedule')
for p in placements:
ep = db.episodes.find_one({'HouseID':p['HouseID']})
if ep:
w.start('Row')
w.element('Time','%s' % p['Start'].strftime('%d/%m/%Y %I:%M:%S %p'))
w.element('ProgID','%s' % ep['Program']['Pili_ID'])
w.element('HouseID','%s' % p['HouseID'])
w.end()
w.close(schedule)
XML文件末尾发生了一件奇怪的事情。我不知道这是从哪里来的。顺便说一句,我在开始和结束之间显示了数千行正确的行:
<?xml version="1.0"?>
<Schedule>
<Row>
<Time>01/12/2011 12:30:00 PM</Time>
<ProgID>5077</ProgID>
<HouseID>AU056563</HouseID>
</Row>
<Row>
<Time>05/12/2011 12:00:00 AM</Time>
<ProgID>5077</ProgID>
<HouseID>AU056563</HouseID>
</Row>
<Row>
<Tim/>
</Row>
</Schedule>
非常感谢您的帮助 我不知道这是否有区别,但在w.close之前是否应该为Schedule元素增加一个w.end?
我可以在输出中看到Schedule元素已关闭,但我也看到了异常行为。可能是拼写错误?这是我的想法,但您能在我的代码中看到错误吗?我不能。模块正在生成Tim。为什么?