我需要我的当前脚本包含额外的xml。这是当前形式的脚本:
import csv
import sys
from xml.etree import ElementTree
from xml.dom import minidom
video_data = ((256, 336000),
(512, 592000),
(768, 848000),
(1128, 1208000))
with open(sys.argv[1], 'rU') as f:
reader = csv.DictReader(f)
for row in reader:
switch_tag = ElementTree.Element('switch')
for suffix, bitrate in video_data:
attrs = {'src': ("mp4:soundcheck/{year}/{id}/{file_root_name}_{suffix}.mp4"
.format(suffix=str(suffix), **row)),
'system-bitrate': str(bitrate),
}
ElementTree.SubElement(switch_tag, 'video', attrs)
print minidom.parseString(ElementTree.tostring(switch_tag)).toprettyxml()
当前输出如下xml:
<?xml version="1.0" ?>
<switch>
<video src="mp4:soundcheck/2006/clay_aiken/02_sc_ca_sorry_256.mp4" system-bitrate="336000"/>
<video src="mp4:soundcheck/2006/clay_aiken/02_sc_ca_sorry_512.mp4" system-bitrate="592000"/>
<video src="mp4:soundcheck/2006/clay_aiken/02_sc_ca_sorry_768.mp4" system-bitrate="848000"/>
<video src="mp4:soundcheck/2006/clay_aiken/02_sc_ca_sorry_1128.mp4" system-bitrate="1208000"/>
</switch>
我需要在xml文件的页眉和页脚中包含额外的信息。请看下面的例子:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE smil PUBLIC "-//W3C//DTD SMIL 2.0//EN" "http://www.w3.org/2001/SMIL20/SMIL20.dtd">
<smil xmlns="http://www.w3.org/2001/SMIL20/Language">
<head>
<meta base="rtmp://cp23636.edgefcs.net/ondemand" />
</head>
<body>
<switch>
<video src="mp4:soundcheck/2006/clay_aiken/02_sc_ca_sorry_256.mp4" system-bitrate="336000"/>
<video src="mp4:soundcheck/2006/clay_aiken/02_sc_ca_sorry_512.mp4" system-bitrate="592000"/>
<video src="mp4:soundcheck/2006/clay_aiken/02_sc_ca_sorry_768.mp4" system-bitrate="848000"/>
<video src="mp4:soundcheck/2006/clay_aiken/02_sc_ca_sorry_1128.mp4" system-bitrate="1208000"/>
</switch>
</body>
</smil>
我如何更新我的当前脚本,以包括这个静态信息以及变量正在从csv在脚本当前的形式拉?
你为什么不把
print minidom.parseString(ElementTree.tostring(switch_tag)).toprettyxml()
print header + ElementTree.tostring(switch_tag) + footer
其中header
和footer
分别是第二个示例输出中开关标签的上方和下方。或者,如果您想使用prettyxml()
,请从其输出中删除第一行。
'n'.join(minidom.parseString(ElementTree.tostring(switch_tag)).toprettyxml().splitlines()[1:])