我正在创建一个web api,需要一个很好的方法来快速生成一些格式良好的xml.我在python中找不到任何好的方法.
注意:有些库看起来很有前途,但要么缺少文档,要么只输出到文件.
ars*_*ars 91
使用lxml:
from lxml import etree
# create XML
root = etree.Element('root')
root.append(etree.Element('child'))
# another child with text
child = etree.Element('child')
child.text = 'some text'
root.append(child)
# pretty string
s = etree.tostring(root, pretty_print=True)
print s
Run Code Online (Sandbox Code Playgroud)
输出:
<root>
<child/>
<child>some text</child>
</root>
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅教程.
Anu*_*yal 88
ElementTree是一个很好的模块,用于读取xml和编写例如
from xml.etree.ElementTree import Element, SubElement, tostring
root = Element('root')
child = SubElement(root, "child")
child.text = "I am a child"
print tostring(root)
Run Code Online (Sandbox Code Playgroud)
输出:
<root><child>I am a child</child></root>
Run Code Online (Sandbox Code Playgroud)
有关详细信息以及如何打印,请参阅本教程.
或者,如果您的XML很简单,请不要低估字符串格式的强大功能:)
xmlTemplate = """<root>
<person>
<name>%(name)s</name>
<address>%(address)s</address>
</person>
</root>"""
data = {'name':'anurag', 'address':'Pune, india'}
print xmlTemplate%data
Run Code Online (Sandbox Code Playgroud)
输出:
<root>
<person>
<name>anurag</name>
<address>Pune, india</address>
</person>
</root>
Run Code Online (Sandbox Code Playgroud)
您也可以使用string.Template或某些模板引擎进行复杂的格式化.
Joh*_*nal 17
我会使用yattag库.我认为这是最蟒蛇的方式:
from yattag import Doc
doc, tag, text = Doc().tagtext()
with tag('food'):
with tag('name'):
text('French Breakfast')
with tag('price', currency='USD'):
text('6.95')
with tag('ingredients'):
for ingredient in ('baguettes', 'jam', 'butter', 'croissants'):
with tag('ingredient'):
text(ingredient)
print(doc.getvalue())
Run Code Online (Sandbox Code Playgroud)
Lar*_*din 15
使用lxml.builder类,来自:http://lxml.de/tutorial.html#the-efactory
import lxml.builder as lb
from lxml import etree
nstext = "new story"
story = lb.E.Asset(
lb.E.Attribute(nstext, name="Name", act="set"),
lb.E.Relation(lb.E.Asset(idref="Scope:767"),
name="Scope", act="set")
)
print 'story:\n', etree.tostring(story, pretty_print=True)
Run Code Online (Sandbox Code Playgroud)
输出:
story:
<Asset>
<Attribute name="Name" act="set">new story</Attribute>
<Relation name="Scope" act="set">
<Asset idref="Scope:767"/>
</Relation>
</Asset>
Run Code Online (Sandbox Code Playgroud)
Ser*_*rgO 10
如果要使用纯Python,可选方式:
ElementTree适用于大多数情况,但它不能 CData和漂亮的打印.
所以,如果你需要CData和漂亮的打印,你应该使用minidom:
minidom_example.py:
from xml.dom import minidom
doc = minidom.Document()
root = doc.createElement('root')
doc.appendChild(root)
leaf = doc.createElement('leaf')
text = doc.createTextNode('Text element with attributes')
leaf.appendChild(text)
leaf.setAttribute('color', 'white')
root.appendChild(leaf)
leaf_cdata = doc.createElement('leaf_cdata')
cdata = doc.createCDATASection('<em>CData</em> can contain <strong>HTML tags</strong> without encoding')
leaf_cdata.appendChild(cdata)
root.appendChild(leaf_cdata)
branch = doc.createElement('branch')
branch.appendChild(leaf.cloneNode(True))
root.appendChild(branch)
mixed = doc.createElement('mixed')
mixed_leaf = leaf.cloneNode(True)
mixed_leaf.setAttribute('color', 'black')
mixed_leaf.setAttribute('state', 'modified')
mixed.appendChild(mixed_leaf)
mixed_text = doc.createTextNode('Do not use mixed elements if it possible.')
mixed.appendChild(mixed_text)
root.appendChild(mixed)
xml_str = doc.toprettyxml(indent=" ")
with open("minidom_example.xml", "w") as f:
f.write(xml_str)
Run Code Online (Sandbox Code Playgroud)
minidom_example.xml:
<?xml version="1.0" ?>
<root>
<leaf color="white">Text element with attributes</leaf>
<leaf_cdata>
<![CDATA[<em>CData</em> can contain <strong>HTML tags</strong> without encoding]]> </leaf_cdata>
<branch>
<leaf color="white">Text element with attributes</leaf>
</branch>
<mixed>
<leaf color="black" state="modified">Text element with attributes</leaf>
Do not use mixed elements if it possible.
</mixed>
</root>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
101931 次 |
最近记录: |