【发布时间】:2018-04-22 16:00:32
【问题描述】:
我有以下 XML 文件:
<tv>
<programme channel="BBC Red Button 1" start="20180422123000 +0000" stop="20180422125500 +0000">
<title lang="en">Live Snooker: The World Championship: Day Two - 2018</title>
<desc lang="en">Coverage of day two at the Crucible Theatre in Sheffield</desc>
<category lang="en">Sport</category>
<icon src="http://images.radiotimes.com/remote/static.radiotimes.com.edgesuite.net/pa/70/26/webANXsnookerlivebbc.jpg?quality=60&mode=crop&width=130&height=100&404=tv" />
</programme>
<programme channel="BBC Red Button 1" start="20180422125500 +0000" stop="20180422150000 +0000">
<title lang="en">Live UEFA Women's Champions League</title>
<desc lang="en">Manchester City v Lyon (Kick-off 1.00pm)</desc>
<category lang="en">Sport</category>
<icon src="http://images.radiotimes.com/assets/images/holding/tv.png?quality=60&mode=crop&width=130&height=100&404=tv" />
</programme>
</tv>
首先我试图删除src等于的元素图标
<icon src="http://images.radiotimes.com/assets/images/holding/tv.png?quality=60&mode=crop&width=130&height=100&404=tv" />
然后对于剩余的图标,我尝试将quality=60&amp;mode=crop&amp;width=130&amp;height=100 替换为quality=100&amp;mode=crop&amp;width=1200&amp;height=723
因此,一旦 XML 文件分阶段,它将如下所示:
<tv>
<programme channel="BBC Red Button 1" start="20180422123000 +0000" stop="20180422125500 +0000">
<title lang="en">Live Snooker: The World Championship: Day Two - 2018</title>
<desc lang="en">Coverage of day two at the Crucible Theatre in Sheffield</desc>
<category lang="en">Sport</category>
<icon src="http://images.radiotimes.com/remote/static.radiotimes.com.edgesuite.net/pa/70/26/webANXsnookerlivebbc.jpg?quality=100&mode=crop&width=1200&height=723&404=tv" />
</programme>
<programme channel="BBC Red Button 1" start="20180422125500 +0000" stop="20180422150000 +0000">
<title lang="en">Live UEFA Women's Champions League</title>
<desc lang="en">Manchester City v Lyon (Kick-off 1.00pm)</desc>
<category lang="en">Sport</category>
</programme>
</tv>
在替换其他值之前,我首先需要删除 XML 文件中不需要的图标,所以我最终不会更改要删除的图标的值,到目前为止我已经尝试过以下删除图标,但我没有成功:
#!/bin/sh
from xml.etree.ElementTree import ElementTree
t = ElementTree()
t.parse('/volume1/TVMosaic/Freeview-WG++/guide.xml')
programmeList = t.findall('tv/programme/icon')
for programmeEl in programmeList:
if programmeEl.attrib['src'] in ('http://images.radiotimes.com/assets/images/holding/tv.png?quality=60&mode=crop&width=130&height=100&404=tv') and \
programmeEl.attrib['src'] == programmeEl.text:
del programmeEl.attrib['src']
t.write('/volume1/TVMosaic/Freeview-WG++/PhasedGuide.xml')
是否有人可以帮助我删除具有我提到的那个 src 的图标,然后用我之前提到的值替换其余图标中的值。
谢谢。
【问题讨论】:
标签: python xml xml-parsing elementtree