简体   繁体   English

包括额外的静态xml与python

[英]including additional static xml with python

I need my current script to include additional xml. 我需要我当前的脚本包含额外的xml。 This is the script in its current form: 这是当前形式的脚本:

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

It currently outputs this xml: 它目前输出这个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>

I need to include additional information in the header and footer of the xml file. 我需要在xml文件的页眉和页脚中包含其他信息。 See example below: 见下面的例子:

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

How can I update my current script to include this static information as well as the variable's that are being pulled from the csv in the scripts current form? 如何更新当前脚本以包含此静态信息以及从脚本当前表单中的csv中提取的变量?

Why don't you replace 你为什么不替换

 print minidom.parseString(ElementTree.tostring(switch_tag)).toprettyxml()

with

 print header + ElementTree.tostring(switch_tag) + footer

Where header and footer are what is above and below the switch tag in your second example output, respectively. 其中headerfooter分别位于第二个示例输出中的开关标记上方和下方。 Alternatively, if you want to use the prettyxml() , remove the first line from its output. 或者,如果要使用prettyxml() ,请从其输出中删除第一行。

 '\n'.join(minidom.parseString(ElementTree.tostring(switch_tag)).toprettyxml().splitlines()[1:])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM