基于CSV单元Python编写XML文件名

基于CSV单元Python编写XML文件名,python,csv,Python,Csv,尝试将此脚本的输出保存到基于csv中的单元格的文件中。我可以调用变量{file\u root\u name}写入xml文件,但不能作为变量写入文件名。如何使用变量file\u root\u name生成文件名 import csv import sys from xml.etree import ElementTree from xml.etree.ElementTree import Element, SubElement, Comment, tostring from xml.dom i

尝试将此脚本的输出保存到基于csv中的单元格的文件中。我可以调用变量
{file\u root\u name}
写入xml文件,但不能作为变量写入文件名。如何使用变量
file\u root\u name
生成文件名

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="  ", 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)

        xml, doc = prettify(root).split('\n', 1)
        output = open('file_root_name'+'.smil', 'w')
        output.write(xml + doctype + doc)
        output.close
导入csv
导入系统
从xml.etree导入元素树
从xml.etree.ElementTree导入元素、子元素、注释到字符串
从xml.dom导入minidom
def美化(要素):
“”“为元素返回打印精美的XML字符串。
"""
rough_string=ElementTree.tostring(元素'utf-8')
重解析=minidom.parseString(粗字符串)
返回重新解析的.toprettyxml(indent=,encoding='utf-8')
doctype=“”
视频数据=((256,336000),
(512, 592000),
(768, 848000),
(1128, 1208000))
将open(sys.argv[1],'rU')作为f:
reader=csv.DictReader(f)
对于读取器中的行:
根=元素('smil')
root.set('xmlns','http://www.w3.org/2001/SMIL20/Language')
head=子元素(根,“head”)
元=子元素(头,'元基='rtmp://cp23636.edgefcs.net/ondemand"')
body=子元素(根“body”)
switch_tag=ElementTree.SubElement(主体“switch”)
对于后缀,视频数据中的比特率:
attrs={'src':(“mp4:soundcheck/{year}/{id}/{file_root_name}{suffix}.mp4”
.format(后缀=str(后缀),**行)),
“系统比特率”:str(比特率),
}
ElementTree.SubElement(开关标签'video',attrs)
xml,doc=prettify(root).split('\n',1)
输出=打开('file\u root\u name'+'.smil','w')
output.write(xml+doctype+doc)
输出。关闭

我不确定我是否遵守了,但如果

  attrs = {'src': ("mp4:soundcheck/{year}/{id}/{file_root_name}_{suffix}.mp4"
                             .format(suffix=str(suffix), **row)),
                     'system-bitrate': str(bitrate),
                     }
然后,“文件\根\名称”必须是dictlike对象行的字符串键。线路

    output = open('file_root_name'+'.smil', 'w')
实际上将字符串“file\u root\u name”与“.smil”组合在一起。所以你真的想要这样的东西

    output = open(row['file_root_name']+'.smil', 'w')
顺便说一句,电话线

    output.close
不会做任何事情--您需要output.close()来代替,或者只是

with open(row['file_root_name']+'.smil', 'w') as output:
    output.write(xml + doctype + doc)

行可以工作,但是如果我尝试打印文件根目录名称,我会得到名称错误:名称“file\u root\u name”没有定义,因此当它在
attrs
中工作时,它不能作为变量工作。它不能工作,因为它不是变量file_root_name'是行字典中的一个键,具有一些相关值(比如“fred”)。如果您想要一个,只需使用
file\u root\u name=row[“file\u root\u name”]