0

I need my current script to include additional 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 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. 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?

1 Answer 1

2

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. Alternatively, if you want to use the prettyxml(), remove the first line from its output.

 '\n'.join(minidom.parseString(ElementTree.tostring(switch_tag)).toprettyxml().splitlines()[1:])
Sign up to request clarification or add additional context in comments.

3 Comments

That is exactly what I want todo but I can't figure out how to make header and footer equal multiple lines with the correct spacing.
Still having problems but this really did help. Thanks @ninjarabbit.
Ah, you need to join the lines after splitting them. Edited. Hope you figured it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.