Node.toprettyxml() 在Python中为DOCTYPE添加换行符
在使用 prettify
的时候,我的文档类型声明(DOCTYPE)被分成了三行。有什么办法可以让它保持在一行吗?
被“分开”的输出:
<?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/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>
脚本:
import csv
import sys
import os.path
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)
file_root_name = row["file_root_name"]
year = row["year"]
id = row["id"]
path = year+'-'+id
file_name = row['file_root_name']+'.smil'
full_path = os.path.join(path, file_name)
output = open(full_path, 'w')
output.write(prettify(doctype, root))
3 个回答
我觉得要去掉Node.toprettyxml
生成的DOCTYPE
中的换行符,至少用Python的方式是做不到的。
问题出在DocumentType
类的writexml
方法,这个方法在minidom
模块的1284行开始,它负责插入那些烦人的换行符。插入的换行符最初来自Node.toprettyxml
方法,然后通过Document
类的writexml
方法传递过去。这个换行符也会传递给其他一些Node
的子类的writexml
方法。如果你在调用Node.prettyxml
时更改换行符,那么输出的XML中使用的换行符也会随之改变。
有一些比较“hack”的方法可以解决这个问题:比如修改你本地的minidom
模块,给DocumentType
类的writexml
方法做个“猴子补丁”,或者在生成XML字符串后处理一下,去掉那些多余的换行符。不过,我觉得这些方法都不太好。
在我看来,最好的办法就是保持现状。DOCTYPE
分成多行真的算是个大问题吗?
我觉得你至少有三种选择:
直接接受这些换行符。虽然它们可能看起来不太好,但其实是合法的。
加一个临时的解决办法,把不好的DOCTYPE替换成一个更好的。你可以试试这样的代码:
import re pretty_xml = prettify(doctype, elem) m = re.search("(<!.*dtd'>)", pretty_xml, re.DOTALL) ugly_doctype = m.group() fixed_xml = pretty_xml.replace(ugly_doctype, doctype)
使用一个功能更强大的XML库。比如说lxml,它和ElementTree大部分是兼容的。使用lxml的
tostring
函数,你就不需要用prettify
函数了,DOCTYPE也能按照你想要的格式输出。举个例子:from lxml import etree doctype = '<!DOCTYPE smil PUBLIC "-//W3C//DTD SMIL 2.0//EN" "http://www.w3.org/2001/SMIL20/SMIL20.dtd">' XML = '<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>' elem = etree.fromstring(XML) print etree.tostring(elem, doctype=doctype, pretty_print=True, xml_declaration=True, encoding="utf-8")
输出结果:
<?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/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>
看过你现在的脚本和你在这个主题上问的其他问题后,我觉得你可以通过字符串处理来简化你的工作,制作smil文件。
你文件里的大部分xml都是固定不变的。你唯一需要认真处理的数据就是video
标签的属性值。为此,标准库里有一个很方便的函数,正好能满足你的需求:xml.sax.saxutils.quoteattr。
所以,考虑到这些点,这里有一个脚本,应该会更容易使用:
import sys, os, csv
from xml.sax.saxutils import quoteattr
smil_header = '''\
<?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>
'''
smil_video = '''\
<video src=%s system-bitrate=%s/>
'''
smil_footer = '''\
</switch>
</body>
</smil>
'''
src_format = 'mp4:soundcheck/%(year)s/%(id)s/%(file_root_name)s_%(suffix)s.mp4'
video_data = (
('256', '336000'), ('512', '592000'),
('768', '848000'), ('1128', '1208000'),
)
root = os.getcwd()
if len(sys.argv) > 2:
root = sys.argv[2]
with open(sys.argv[1], 'rU') as stream:
for row in csv.DictReader(stream):
smil = [smil_header]
for suffix, bitrate in video_data:
row['suffix'] = suffix
smil.append(smil_video % (
quoteattr(src_format) % row, quoteattr(bitrate)
))
smil.append(smil_footer)
directory = os.path.join(root, '%(year)s-%(id)s' % row)
try:
os.makedirs(directory)
except OSError:
pass
path = os.path.join(directory, '%(file_root_name)s.smil' % row)
print ':: writing file:', path
with open(path, 'wb') as stream:
stream.write(''.join(smil))