我在包含xml的数据库中有很多行,并且我正在尝试编写一个Python脚本,该脚本将遍历这些行并计算出现特定节点属性的实例数量。 例如,我的树看起来像:

<foo><bar><type foobar="1"/><type foobar="2"/></bar>
</foo>

如何使用Python访问XML中的属性1和2?


#1楼

这是使用cElementTree的非常简单但有效的代码。

try:import cElementTree as ET
except ImportError:try:# Python 2.5 need to import a different moduleimport xml.etree.cElementTree as ETexcept ImportError:exit_err("Failed to import cElementTree from any known place")      def find_in_tree(tree, node):found = tree.find(node)if found == None:print "No %s in file" % nodefound = []return found  # Parse a xml file (specify the path)
def_file = "xml_file_name.xml"
try:dom = ET.parse(open(def_file, "r"))root = dom.getroot()
except:exit_err("Unable to open and parse input definition file: " + def_file)# Parse to find the child nodes list of node 'myNode'
fwdefs = find_in_tree(root,"myNode")

资源:

http://www.snip2code.com/Snippet/991/python-xml-parse?fromPage=1


#2楼

您可以使用BeautifulSoup

from bs4 import BeautifulSoupx="""<foo><bar><type foobar="1"/><type foobar="2"/></bar>
</foo>"""y=BeautifulSoup(x)
>>> y.foo.bar.type["foobar"]
u'1'>>> y.foo.bar.findAll("type")
[<type foobar="1"></type>, <type foobar="2"></type>]>>> y.foo.bar.findAll("type")[0]["foobar"]
u'1'
>>> y.foo.bar.findAll("type")[1]["foobar"]
u'2'

#3楼

我建议ElementTree 。 相同的API还有其他兼容的实现,例如Python标准库本身中的lxmlcElementTree 。 但是,在这种情况下,他们主要添加的是更高的速度-编程的难易程度取决于ElementTree定义的API。

首先从XML构建一个Element实例root ,例如,使用XML函数,或者通过解析文件,例如:

import xml.etree.ElementTree as ET
root = ET.parse('thefile.xml').getroot()

ElementTree显示的许多其他方式中的任何一种。 然后执行以下操作:

for type_tag in root.findall('bar/type'):value = type_tag.get('foobar')print(value)

和类似的,通常很简单的代码模式。


#4楼

Python具有与expat xml解析器的接口。

xml.parsers.expat

这是一个非验证解析器,因此不会发现错误的xml。 但是,如果您知道文件是正确的,那么这很好,您可能会获得所需的确切信息,并且可以随时丢弃其余信息。

stringofxml = """<foo><bar><type arg="value" /><type arg="value" /><type arg="value" /></bar><bar><type arg="value" /></bar>
</foo>"""
count = 0
def start(name, attr):global countif name == 'type':count += 1p = expat.ParserCreate()
p.StartElementHandler = start
p.Parse(stringofxml)print count # prints 4

#5楼

我发现Python xml.domxml.dom.minidom非常简单。 请记住,DOM不适用于大量XML,但是如果您的输入很小,则可以正常工作。


#6楼

minidom是最快,最简单的方法:

XML:

<data><items><item name="item1"></item><item name="item2"></item><item name="item3"></item><item name="item4"></item></items>
</data>

蟒蛇:

from xml.dom import minidom
xmldoc = minidom.parse('items.xml')
itemlist = xmldoc.getElementsByTagName('item')
print(len(itemlist))
print(itemlist[0].attributes['name'].value)
for s in itemlist:print(s.attributes['name'].value)

输出值

4
item1
item1
item2
item3
item4

#7楼

lxml.objectify非常简单。

以您的示例文本:

from lxml import objectify
from collections import defaultdictcount = defaultdict(int)root = objectify.fromstring(text)for item in root.bar.type:count[item.attrib.get("foobar")] += 1print dict(count)

输出:

{'1': 1, '2': 1}

#8楼

有很多选择。 如果速度和内存使用成为问题,则cElementTree看起来很棒。 与仅使用readlines读取文件相比,它的开销很小。

可以从cElementTree网站复制的下表中找到相关指标:

library                         time    space
xml.dom.minidom (Python 2.1)    6.3 s   80000K
gnosis.objectify                2.0 s   22000k
xml.dom.minidom (Python 2.4)    1.4 s   53000k
ElementTree 1.2                 1.6 s   14500k
ElementTree 1.2.4/1.3           1.1 s   14500k
cDomlette (C extension)         0.540 s 20500k
PyRXPU (C extension)            0.175 s 10850k
libxml2 (C extension)           0.098 s 16000k
readlines (read as utf-8)       0.093 s 8850k
cElementTree (C extension)  --> 0.047 s 4900K <--
readlines (read as ascii)       0.032 s 5050k

正如@jfs指出的那样 , cElementTree与Python捆绑在一起:

  • Python 2: from xml.etree import cElementTree as ElementTree
  • Python 3: from xml.etree import ElementTree (自动使用加速的C版本)。

#9楼

为了简单起见,我建议使用xmltodict 。

它将您的xml解析为OrderedDict;

>>> e = '<foo><bar><type foobar="1"/><type foobar="2"/></bar></foo> '>>> import xmltodict
>>> result = xmltodict.parse(e)
>>> resultOrderedDict([(u'foo', OrderedDict([(u'bar', OrderedDict([(u'type', [OrderedDict([(u'@foobar', u'1')]), OrderedDict([(u'@foobar', u'2')])])]))]))])>>> result['foo']OrderedDict([(u'bar', OrderedDict([(u'type', [OrderedDict([(u'@foobar', u'1')]), OrderedDict([(u'@foobar', u'2')])])]))])>>> result['foo']['bar']OrderedDict([(u'type', [OrderedDict([(u'@foobar', u'1')]), OrderedDict([(u'@foobar', u'2')])])])

#10楼

import xml.etree.ElementTree as ET
data = '''<foo><bar><type foobar="1"/><type foobar="2"/></bar></foo>'''
tree = ET.fromstring(data)
lst = tree.findall('bar/type')
for item in lst:print item.get('foobar')

这将打印foobar属性的值。


#11楼

为了增加另一种可能性,您可以使用untangle ,因为它是一个简单的xml-to-python-object库。 这里有一个例子:

安装

pip install untangle

用法

您的xml文件(稍作更改):

<foo><bar name="bar_name"><type foobar="1"/></bar>
</foo>

使用untangle访问属性:

import untangleobj = untangle.parse('/path_to_xml_file/file.xml')print obj.foo.bar['name']
print obj.foo.bar.type['foobar']

输出将是:

bar_name
1

关于解缠的更多信息可以在这里找到。
另外(如果您感到好奇),您可以在此处找到用于XML和Python的工具列表(您还将看到以前的答案提到了最常见的工具)。


#12楼

我可能会建议declxml 。

全面披露:我之所以编写此库,是因为我正在寻找一种无需在ElementTree中编写数十行命令式解析/序列化代码的方法即可在XML和Python数据结构之间进行转换的方法。

使用declxml,您可以使用处理器以声明方式定义XML文档的结构以及如何在XML和Python数据结构之间进行映射。 处理器用于序列化和解析以及基本的验证。

解析为Python数据结构非常简单:

import declxml as xmlxml_string = """
<foo><bar><type foobar="1"/><type foobar="2"/></bar>
</foo>
"""processor = xml.dictionary('foo', [xml.dictionary('bar', [xml.array(xml.integer('type', attribute='foobar'))])
])xml.parse_from_string(processor, xml_string)

产生输出:

{'bar': {'foobar': [1, 2]}}

您还可以使用同一处理器将数据序列化为XML

data = {'bar': {'foobar': [7, 3, 21, 16, 11]
}}xml.serialize_to_string(processor, data, indent='    ')

产生以下输出

<?xml version="1.0" ?>
<foo><bar><type foobar="7"/><type foobar="3"/><type foobar="21"/><type foobar="16"/><type foobar="11"/></bar>
</foo>

如果要使用对象而不是字典,则可以定义处理器以将数据与对象之间进行转换。

import declxml as xmlclass Bar:def __init__(self):self.foobars = []def __repr__(self):return 'Bar(foobars={})'.format(self.foobars)xml_string = """
<foo><bar><type foobar="1"/><type foobar="2"/></bar>
</foo>
"""processor = xml.dictionary('foo', [xml.user_object('bar', Bar, [xml.array(xml.integer('type', attribute='foobar'), alias='foobars')])
])xml.parse_from_string(processor, xml_string)

产生以下输出

{'bar': Bar(foobars=[1, 2])}

#13楼

XML格式

<foo><bar><type foobar="1"/><type foobar="2"/></bar>
</foo>

PYTHON_CODE

import xml.etree.cElementTree as ETtree = ET.parse("foo.xml")
root = tree.getroot()
root_tag = root.tag
print(root_tag) for form in root.findall("./bar/type"):x=(form.attrib)z=list(x)for i in z:print(x[i])

输出:

foo
1
2

#14楼

xml.etree.ElementTree与lxml

这些是我在使用它们之间进行选择之前会了解的两个最常用的库的优点。

xml.etree.ElementTree:

  1. 来自标准库 :无需安装任何模块

xml文件

  1. 轻松编写XML声明 :例如,您是否需要添加standalone="no"
  2. 印刷精美:无需额外代码即可拥有漂亮的缩进 XML。
  3. 对象化功能:它使您可以像处理普通的Python对象层次结构.node一样使用XML。

#15楼

如果您使用python-benedict ,则无需使用特定于lib的api,只需从xml初始化一个新实例并轻松对其进行管理即可(因为它是dict子类)。

安装: pip install python-benedict

from benedict import benedict as bdict# data-source can be an url, a filepath or data-string (as in this example)
data_source = """
<foo><bar><type foobar="1"/><type foobar="2"/></bar>
</foo>"""data = bdict.from_xml(data_source)
t_list = data['foo.bar'] # yes, keypath supported
for t in t_list:print(t['@foobar'])

它支持多种格式的I / O操作并对其进行规范化: Base64CSVJSONTOMLXMLYAMLquery-string

它在GitHub上经过了良好的测试和开源: https : //github.com/fabiocaccamo/python-benedict

如何在Python中解析XML?相关推荐

  1. 面试官问我:如何在 Python 中解析和修改 XML

    摘要:我们经常需要解析用不同语言编写的数据.Python提供了许多库来解析或拆分用其他语言编写的数据.在此 Python XML 解析器教程中,您将学习如何使用 Python 解析 XML. 本文分享 ...

  2. 如何在Python中解析YAML文件

    如何在Python中解析YAML文件? #1楼 不依赖C标头的最简单,最纯净的方法是PyYaml( 文档 ): #!/usr/bin/env pythonimport yamlwith open(&q ...

  3. python 日期解析_如何在Python中解析ISO 8601格式的日期?

    python 日期解析 Python provides a datetime standard library which introduces datetime.isoformat(). As pe ...

  4. python 处理xml pandas_在python中解析xml到pandas数据帧

    解决方案中的问题是"元素数据提取"没有正确完成.您在问题中提到的xml嵌套在几个层中.这就是为什么我们需要递归地读取和提取数据.在这种情况下,下面的解决方案应该能满足您的需要.尽管 ...

  5. 教你如何在Python中读,写和解析CSV文

    摘要:在这篇文章中关于"在Python如何阅读CSV文件"中,我们将学习如何读,写和解析的CSV文件的Python. 您知道将表格数据存储到纯文本文件背后的机制是什么吗?答案是CS ...

  6. java opencsv 乱码_教你如何在Python中读,写和解析CSV文

    摘要:在这篇文章中关于"在Python如何阅读CSV文件"中,我们将学习如何读,写和解析的CSV文件的Python. 您知道将表格数据存储到纯文本文件背后的机制是什么吗?答案是CS ...

  7. Python中的XML解析错误[Et.parse(xml) ‘gbk‘ codec can‘t decode byte]分析与解决

    Python中的XML解析错误[Et.parse(xml) 'gbk' codec can't decode byte]分析与解决 在Python开发中,我们经常会使用XML格式的数据来进行数据传输或 ...

  8. 如何在Python中捕获SIGINT?

    我正在研究启动多个进程和数据库连接的python脚本. 我不时地想用Ctrl + C信号杀死脚本,我想进行一些清理. 在Perl中,我可以这样做: $SIG{'INT'} = 'exit_gracef ...

  9. eval在python中是什么意思_如何在Python中使用eval ?

    Python中的 eval是什么? 在Python中,我们有许多内置方法,这些方法对于使Python成为所有人的便捷语言至关重要,而eval是其中一种.eval函数的语法如下: eval(expres ...

最新文章

  1. Flutter开发之实现沉浸式状态栏的效果
  2. mysql查询一个数据库所有表的记录数,mysql 查看数据库中所有表的记录数
  3. 安装Node和NPM
  4. 作为程序员,我在电脑上都装过哪些 Chrome 插件?
  5. 《魔鬼搭讪学》读书笔记
  6. @Autowired的使用--Spring规范解释,推荐对构造函数进行注释
  7. glide加载gif图不显示动画_Android使用glide加载gif动画设置播放次数
  8. QQ炫舞新上线紫钻Lv9和皇冠贵族Lv11 QQ炫舞紫钻9级图标上线
  9. 电脑开机加速,一下子就提升了20几秒
  10. Fluent残差图中continuity不收敛如何处理
  11. 产品经理知识体系专题
  12. xbox one x驱动_如何在Windows,OS X和Linux上使用Xbox One控制器
  13. ant安装和ant的环境配置
  14. 摸索 Jenkins 的自动维护方案
  15. 新电脑安装python环境
  16. 缺少lib库文件解决方法
  17. 让一切都过去吧,高手挑战2过关方法
  18. 二进制转换成十进制然后输出 学堂在线第二章作业2-2
  19. IdentityServer4实战详解
  20. 【软件体系结构】考点总结 第三章 软件体系结构风格 XJU

热门文章

  1. 定义或修改本地屏幕元素的注释
  2. 何时创建MVC应用程序
  3. JS——构造函数、原型与实例之间的关系 及 原型链 的描述
  4. RHEL 7中防火墙的配置和使用
  5. Codeforces Round #257 (Div. 2) C. Jzzhu and Chocolate
  6. 四、java多线程核心技术——synchronized同步方法与synchronized同步快
  7. getHibernateTemplate()为NUll
  8. Android Stuido 快速设置成eclipse的快捷键习惯
  9. 大数据建模,eBay的一个牛人
  10. PGPDesktopWin32-10.2.0 加密与签名-软件实验8