CSV使用Python将行转换为XML文件

时间:2011-12-29 20:12:57

标签: python xml csv

我的csv文件看起来像这样:

artist,year,id,video_name,new_video_id,file_root_name,video_type
,,,,,,
Clay Aiken,1,clay_aiken,Sorry Seems To Be...,sorry-seems-to-be,02_sc_ca_sorry,FLV
Clay Aiken,1,clay_aiken,Everything I Do (I Do It For You),everything-i-do-i-do-it-for-you,03_sc_ca_everything,FLV
Clay Aiken,1,clay_aiken,A Thousand Days,a-thousand-days,04_sc_ca_thousandda,FLV
Clay Aiken,1,clay_aiken,Here You Come Again,here-you-come-again,05_sc_ca_hereyoucom,FLV
Clay Aiken,1,clay_aiken,Interview,interview,06_sc_ca_intv,FLV

上面的每一行都会生成一个单独的xml文件,如下所示(准确地说是5):

<?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/%year/%id/%file_root_name_256.mp4" system-bitrate="336000"/>
      <video src="mp4:soundcheck/%year/%id/%file_root_name_512.mp4" system-bitrate="592000"/>
      <video src="mp4:soundcheck/%year/%id/%file_root_name_768.mp4" system-bitrate="848000"/>
      <video src="mp4:soundcheck/%year/%id/%file_root_name_1128.mp4" system-bitrate="1208000"/>
    </switch>
  </body>
</smil>

将其命名为%new_video_id.smil

我已经弄清楚如何解析csv文件:

import csv
import sys

f = open(sys.argv[1], 'rU')
reader = csv.reader(f)
for row in reader:
    year = row[1]
    id = row[2]
    file_root_name = row[5]
    print year, id, file_root_name

如何在编写xml文件时获取每个变量并包含?

2 个答案:

答案 0 :(得分:4)

我会从这样的事情开始:

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

基本上,在解析csv文件时,会使用行中的属性创建一个xml文档,以逐个创建视频标记。

示例输出(对于一行):

<?xml version="1.0" ?>
<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>

注意:ElementTree不支持漂亮的打印,所以我使用了PyMOTW中解释的技巧。

答案 1 :(得分:1)

我会考虑将您的XML模板视为格式字符串:创建一个值为XML的字符串,替换所有%year%id%file_root_name。使用%s,然后你就可以:

 print xml_template % [year, id, file_root_name] * 3

如果您的csv中的数据包含不需要转义的XML合法字符,那么这只会起作用。您需要预处理每个值以将标记字符(<>'")转换为实体(&lt;&gt;&apos;&quot;)。

使用minidom和ElementTree来构建XML更安全,正如jcollado建议的那样。