python 在python中模板xml文件的快速简便的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1055108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
fast and easy way to template xml files in python
提问by user130085
Right now I've hard coded the whole xml file in my python script and just doing out.write(), but now it's getting harder to manage because i have multiple types of xml file.
现在我已经在我的 python 脚本中对整个 xml 文件进行了硬编码,只是在执行 out.write(),但是现在它越来越难以管理,因为我有多种类型的 xml 文件。
What is the easiest and quickest way to setup templating so that I can just give the variable names amd filename?
什么是设置模板的最简单快捷的方法,以便我可以只给出变量名和文件名?
采纳答案by zooglash
You asked for the easiest and quickest, so see this post: http://blog.simonwillison.net/post/58096201893/simpletemplates
你要求最简单和最快的,所以看这篇文章:http: //blog.simonwillison.net/post/58096201893/simpletemplates
If you want something smarter, take a look here.
如果您想要更智能的东西,请看这里。
回答by S.Lott
回答by gimel
A lightweight option is xml.dom.minidom
一个轻量级的选项是xml.dom.minidom
xml.dom.minidom is a light-weight implementation of the Document Object Model interface. It is intended to be simpler than the full DOM and also significantly smaller.
xml.dom.minidom 是文档对象模型接口的轻量级实现。它旨在比完整的 DOM 更简单,而且体积也小得多。
You can create DOM object using the xml.dom
API, for example DOM Element objects, and generate the XML using Node.writexml
. Note that this requires building DOM hierarchies, which may not be what you are after.
您可以使用xml.dom
API创建 DOM 对象,例如DOM 元素对象,并使用 生成 XML Node.writexml
。请注意,这需要构建 DOM 层次结构,这可能不是您所追求的。
more pythonic option is ElementTree.
更多 pythonic 选项是ElementTree。
The Element type is a flexible container object, designed to store hierarchical data structures in memory. The type can be described as a cross between a list and a dictionary.
Element 类型是一个灵活的容器对象,旨在在内存中存储分层数据结构。该类型可以描述为列表和字典之间的交叉。
ElementTree objects are easier to create and handle in Python
, and can be serialized to XML with ElementTree.dump()or ElementTree.tostring()
ElementTree 对象在 中更容易创建和处理Python
,并且可以使用ElementTree.dump()或ElementTree.tostring()序列化为 XML
回答by joej
Short answer is:You should be focusing, and dealing with, the data (i.e., python object) and not the raw XML
简短的回答是:您应该关注和处理数据(即 python 对象)而不是原始 XML
Basic story:XML is supposed to be a representation of some data, or data set. You don't have a lot of detail in your question about the type of data, what it represents, etc, etc -- so I'll give you some basic answers.
基本故事:XML 应该是一些数据或数据集的表示。您的问题中没有关于数据类型、它代表什么等的很多细节——所以我会给你一些基本的答案。
Python choices:BeautifulSoup, lxml and other python libraries (ElementTree, etc.), make dealing with XML more easy. They let me read in, or write out, XML data much more easily than if I'd tried to work directly with the XML in raw form.
Python 选择:BeautifulSoup、lxml 和其他 Python 库(ElementTree 等),使处理 XML 更容易。它们让我读入或写出 XML 数据比尝试直接使用原始格式的 XML 容易得多。
In the middle of those 2 (input,output) activities, my python program is dealing with a nice python object or some kind of parse tree I can walk. You can read data in, create an object from that string, manipulate it and write out XML.
在这 2 个(输入、输出)活动的中间,我的 python 程序正在处理一个不错的 python 对象或某种我可以走的解析树。您可以读入数据,从该字符串创建一个对象,操作它并写出 XML。
Other choice, Templates:OK -- maybe you like XML and just want to "template" it so you can populate it with the data.
其他选择,模板:好的——也许您喜欢 XML 并且只想“模板化”它以便您可以用数据填充它。
You might be more comfortable with this, if you aren't really manipulating the data -- but just representing it for output. And, this is similar to the XML strings you are currently using -- so may be more familiar.
如果您不是真正操作数据,您可能会更适应这种方式——而只是将其表示为输出。而且,这类似于您当前使用的 XML 字符串——因此可能更熟悉。
Use Cheetah, Jinja, or other template libraries to help. Make a template for the XML file, using that template language.
使用 Cheetah、Jinja 或其他模板库来提供帮助。使用该模板语言为 XML 文件制作模板。
For example, you just read a list of books from a file or database table. You would pass this list of book objects to the template engine, with a template, and then tell it to write out your XML output.
例如,您只是从文件或数据库表中读取书籍列表。您将使用模板将此书对象列表传递给模板引擎,然后告诉它写出您的 XML 输出。
Example template for these book objects:
这些书籍对象的示例模板:
<?xml version="1.0"?>
<catalog>
{% for object in object_list %}
<book id="{{ object.bookID }}">
<author>{{ object.author_name }}</author>
<title>{{ object.title }}</title>
<genre>{{ object.genre }}</genre>
<price>{{ object.price }}</price>
<publish_date>{{ object.pub_date }}</publish_date>
<description>{{ object.description }}</description>
</book>
{% endfor %}
</catalog>
</xml>
The template engine would loop through the "object_list" and output a long XML file with all your books. That would be muchbetter than storing raw XML strings, as you currently are.
模板引擎将遍历“object_list”并输出一个包含所有书籍的长 XML 文件。这比存储原始 XML 字符串要好得多,就像您目前那样。
This makes the update & modification of the display of XML separate from the data, data storage, and data manipulation -- making your life easier.
这使得 XML 显示的更新和修改与数据、数据存储和数据操作分离——让您的生活更轻松。
回答by Alex Martelli
My ancient YAPTUand Palmer's yaptoovariant on it should be usable if you want something very simple and lightweight -- but there are many, many other general and powerful templating engines to chose among, these days. A pretty complete list is here.
如果你想要一些非常简单和轻量的东西,我古老的YAPTU和 Palmer 的yaptoo变体应该可以使用——但是现在还有许多其他通用和强大的模板引擎可供选择。一个非常完整的列表在这里。