【发布时间】:2012-05-28 11:26:24
【问题描述】:
请查看下面的代码,我正在使用它来使用 python 生成 xml。
from lxml import etree
# Some dummy text
conn_id = 5
conn_name = "Airtelll"
conn_desc = "Largets TRelecome"
ip = "192.168.1.23"
# Building the XML tree
# Note how attributes and text are added, using the Element methods
# and not by concatenating strings as in your question
root = etree.Element("ispinfo")
child = etree.SubElement(root, 'connection',
number = str(conn_id),
name = conn_name,
desc = conn_desc)
subchild_ip = etree.SubElement(child, 'ip_address')
subchild_ip.text = ip
# and pretty-printing it
print etree.tostring(root, pretty_print=True)
这将产生:
<ispinfo>
<connection desc="Largets TRelecome" number="5" name="Airtelll">
<ip_address>192.168.1.23</ip_address>
</connection>
</ispinfo>
但我希望它是这样的:
<ispinfo>
<connection desc="Largets TRelecome" number='1' name="Airtelll">
<ip_address>192.168.1.23</ip_address>
</connection>
</ispinfo>
平均数字属性应该用单引号括起来。任何想法....我怎样才能做到这一点
【问题讨论】:
-
如果需要任何澄清,请告诉我
-
你的意思是你希望属性值用单引号而不是双引号?
-
如果“逗号”的意思是“引号”,而“1”的意思是“5”,那么您不必在意。两者在 XML 中没有语义上的区别。
-
@janne 正是我想要单引号而不是双引号中的属性值
-
之前已经在PYTHON 2.6 XML.ETREE to output single quote for attributes instead of double quote 中询问过,但正如 larsmans 所写,你不应该在意。
标签: python xml xml-parsing