使用Python和XML在`for`循环中拆分SubElements

时间:2011-12-30 20:30:24

标签: python xml

我的代码差不多完成但是我在for循环中拆分SubElements时遇到了麻烦(?)如何在xml根节点中将每个元素都放到自己的代码中?

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)

返回:

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

但我想要的是每个元素都是独立的。像这样:

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

<?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 个答案:

答案 0 :(得分:2)

喜欢这个吗?

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)