Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6992817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:41:03+00:00 2026-05-27T19:41:03+00:00

My code is nearly finished but I’m having trouble with splitting up the SubElements

  • 0

My code is nearly finished but I’m having trouble with splitting up the SubElements in my for loop (?) How can I get each element into its own within the xml root node?

import csv
import sys

from xml.etree import ElementTree
from xml.etree.ElementTree import Element, SubElement, Comment, tostring

from xml.dom import minidom

def prettify(elem):
    """Return a pretty-printed XML string for the Element.
    """
    rough_string = ElementTree.tostring(elem, 'utf-8')
    reparsed = minidom.parseString(rough_string)
    return reparsed.toprettyxml(indent="  ")

xml = '<?xml version="1.0" encoding="utf-8"?>'
doctype = '<!DOCTYPE smil PUBLIC "-//W3C//DTD SMIL 2.0//EN" "http://www.w3.org/2001/SMIL20/SMIL20.dtd">'
root = Element('smil')
root.set('xmlns', 'http://www.w3.org/2001/SMIL20/Language')              
head = SubElement(root, 'head')
meta = SubElement(head, 'meta base="rtmp://cp23636.edgefcs.net/ondemand"')
body = SubElement(root, 'body')

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.SubElement(body, '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 xml + doctype + prettify(root)

returns:

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE smil PUBLIC "-//W3C//DTD SMIL 2.0//EN" "http://www.w3.org/2001/SMIL20/SMIL20.dtd"><?xml version="1.0" ?>
<smil xmlns="http://www.w3.org/2001/SMIL20/Language">
  <head>
    <meta base="rtmp://cp23636.edgefcs.net/ondemand"/>
  </head>
  <body>
    <switch>
      <video src="mp4:soundcheck/1/clay_aiken/02_sc_ca_sorry_256.mp4" system-bitrate="336000"/>
      <video src="mp4:soundcheck/1/clay_aiken/02_sc_ca_sorry_512.mp4" system-bitrate="592000"/>
      <video src="mp4:soundcheck/1/clay_aiken/02_sc_ca_sorry_768.mp4" system-bitrate="848000"/>
      <video src="mp4:soundcheck/1/clay_aiken/02_sc_ca_sorry_1128.mp4" system-bitrate="1208000"/>
    </switch>
    <switch>
      <video src="mp4:soundcheck/1/clay_aiken/03_sc_ca_everything_256.mp4" system-bitrate="336000"/>
      <video src="mp4:soundcheck/1/clay_aiken/03_sc_ca_everything_512.mp4" system-bitrate="592000"/>
      <video src="mp4:soundcheck/1/clay_aiken/03_sc_ca_everything_768.mp4" system-bitrate="848000"/>
      <video src="mp4:soundcheck/1/clay_aiken/03_sc_ca_everything_1128.mp4" system-bitrate="1208000"/>
    </switch>
    <switch>
      <video src="mp4:soundcheck/1/clay_aiken/04_sc_ca_thousandda_256.mp4" system-bitrate="336000"/>
      <video src="mp4:soundcheck/1/clay_aiken/04_sc_ca_thousandda_512.mp4" system-bitrate="592000"/>
      <video src="mp4:soundcheck/1/clay_aiken/04_sc_ca_thousandda_768.mp4" system-bitrate="848000"/>
      <video src="mp4:soundcheck/1/clay_aiken/04_sc_ca_thousandda_1128.mp4" system-bitrate="1208000"/>
    </switch>
    <switch>
      <video src="mp4:soundcheck/1/clay_aiken/05_sc_ca_hereyoucom_256.mp4" system-bitrate="336000"/>
      <video src="mp4:soundcheck/1/clay_aiken/05_sc_ca_hereyoucom_512.mp4" system-bitrate="592000"/>
      <video src="mp4:soundcheck/1/clay_aiken/05_sc_ca_hereyoucom_768.mp4" system-bitrate="848000"/>
      <video src="mp4:soundcheck/1/clay_aiken/05_sc_ca_hereyoucom_1128.mp4" system-bitrate="1208000"/>
    </switch>
    <switch>
      <video src="mp4:soundcheck/1/clay_aiken/06_sc_ca_intv_256.mp4" system-bitrate="336000"/>
      <video src="mp4:soundcheck/1/clay_aiken/06_sc_ca_intv_512.mp4" system-bitrate="592000"/>
      <video src="mp4:soundcheck/1/clay_aiken/06_sc_ca_intv_768.mp4" system-bitrate="848000"/>
      <video src="mp4:soundcheck/1/clay_aiken/06_sc_ca_intv_1128.mp4" system-bitrate="1208000"/>
    </switch>
  </body>
</smil>

But what I want is for each element to be on its own. Like this:

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE smil PUBLIC "-//W3C//DTD SMIL 2.0//EN" "http://www.w3.org/2001/SMIL20/SMIL20.dtd"><?xml version="1.0" ?>
<smil xmlns="http://www.w3.org/2001/SMIL20/Language">
  <head>
    <meta base="rtmp://cp23636.edgefcs.net/ondemand"/>
  </head>
  <body>
    <switch>
      <video src="mp4:soundcheck/1/clay_aiken/02_sc_ca_sorry_256.mp4" system-bitrate="336000"/>
      <video src="mp4:soundcheck/1/clay_aiken/02_sc_ca_sorry_512.mp4" system-bitrate="592000"/>
      <video src="mp4:soundcheck/1/clay_aiken/02_sc_ca_sorry_768.mp4" system-bitrate="848000"/>
      <video src="mp4:soundcheck/1/clay_aiken/02_sc_ca_sorry_1128.mp4" system-bitrate="1208000"/>
    </switch>
   </body>
</smil>

and

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE smil PUBLIC "-//W3C//DTD SMIL 2.0//EN" "http://www.w3.org/2001/SMIL20/SMIL20.dtd"><?xml version="1.0" ?>
<smil xmlns="http://www.w3.org/2001/SMIL20/Language">
  <head>
    <meta base="rtmp://cp23636.edgefcs.net/ondemand"/>
  </head>
  <body>
    <switch>
      <video src="mp4:soundcheck/1/clay_aiken/03_sc_ca_everything_256.mp4" system-bitrate="336000"/>
      <video src="mp4:soundcheck/1/clay_aiken/03_sc_ca_everything_512.mp4" system-bitrate="592000"/>
      <video src="mp4:soundcheck/1/clay_aiken/03_sc_ca_everything_768.mp4" system-bitrate="848000"/>
      <video src="mp4:soundcheck/1/clay_aiken/03_sc_ca_everything_1128.mp4" system-bitrate="1208000"/>
    </switch>
  </body>
</smil>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:41:04+00:00Added an answer on May 27, 2026 at 7:41 pm

    Like this?

    import csv
    import sys
    
    from xml.etree import ElementTree
    from xml.etree.ElementTree import Element, SubElement, Comment, tostring
    
    from xml.dom import minidom
    
    def prettify(doctype, elem):
        """Return a pretty-printed XML string for the Element.
        """
        rough_string = doctype + ElementTree.tostring(elem, 'utf-8')
        reparsed = minidom.parseString(rough_string)
        return reparsed.toprettyxml(indent="  ", encoding = 'utf-8')
    
    doctype = '<!DOCTYPE smil PUBLIC "-//W3C//DTD SMIL 2.0//EN" "http://www.w3.org/2001/SMIL20/SMIL20.dtd">'
    
    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:
            root = Element('smil')
            root.set('xmlns', 'http://www.w3.org/2001/SMIL20/Language')
            head = SubElement(root, 'head')
            meta = SubElement(head, 'meta base="rtmp://cp23636.edgefcs.net/ondemand"')
            body = SubElement(root, 'body')
    
            switch_tag = ElementTree.SubElement(body, '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 prettify(doctype, root)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have nearly finished writing this code: http://jsfiddle.net/2Jkk9/11/ but I can't figure out how
Quick googling result that QR code can hold nearly 3kb (8 bit) data. But
I'm new to Django but I seem to have nearly identical code working on
I have an app that is nearly finished but encountered an annoying problem. In
I have nearly finished my app but still have to make sure landscape orientations
Description: I've got a project which is nearly finished now, but I've noticed the
I'm nearly finished with this project but I have been beating my head against
The code below is working nearly flawlessly, however my value for page title on
I am learning RX (Reactive Extensions), and have found someone posted some code nearly
I have a couple classes that have nearly identical code. Only a string or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.