Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中使用lxml是否有一种优雅的方法来计算xml文件中的标记元素?_Python_Xml_Tags_Count_Lxml - Fatal编程技术网

在python中使用lxml是否有一种优雅的方法来计算xml文件中的标记元素?

在python中使用lxml是否有一种优雅的方法来计算xml文件中的标记元素?,python,xml,tags,count,lxml,Python,Xml,Tags,Count,Lxml,我可以将xml文件的内容读入字符串,并使用字符串操作来实现这一点,但我想有一种更优雅的方法来实现这一点。由于我在文件中没有找到任何线索,我在这里搜索: 给定一个xml(见下文)文件,如何计算xml标记的数量,就像示例中的作者标记的数量一样,bewlow是最优雅的方式?我们假设每个作者只出现一次 <root> <author>Tim</author> <author>Eva</author> <author&

我可以将xml文件的内容读入字符串,并使用字符串操作来实现这一点,但我想有一种更优雅的方法来实现这一点。由于我在文件中没有找到任何线索,我在这里搜索:

给定一个xml(见下文)文件,如何计算xml标记的数量,就像示例中的作者标记的数量一样,bewlow是最优雅的方式?我们假设每个作者只出现一次

<root>
    <author>Tim</author>
    <author>Eva</author>
    <author>Martin</author>
    etc.
</root>

提姆
伊娃
马丁
等

此xml文件很简单,但有可能作者并不总是一个接一个地列出,他们之间可能有其他标记。

使用with。

如果要计算所有作者标记:

import lxml.etree
doc = lxml.etree.parse(xml)
count = doc.xpath('count(//author)')

使用modulere处理SGML/XML/HTML文本时必须小心,因为并非所有此类文件的处理都不能用regex执行(regex不能解析SGML/HTML/XML文本)

但在这里,在这个特定的问题中,我认为这是可能的(re.DOTALL是强制性的,因为一个元素可以扩展到多行;除此之外,我无法想象任何其他可能的陷阱)

from time import clock
n= 10000
print 'n ==',n,'\n'



import lxml.etree
doc = lxml.etree.parse('xml.txt')

te = clock()
for i in xrange(n):
    countlxml = doc.xpath('count(//author)')
tf = clock()
print 'lxml\ncount:',countlxml,'\n',tf-te,'seconds'



import re
with open('xml.txt') as f:
    ch = f.read()

regx = re.compile('<author>.*?</author>',re.DOTALL)
te = clock()
for i in xrange(n):
    countre = sum(1 for mat in regx.finditer(ch))
tf = clock()
print '\nre\ncount:',countre,'\n',tf-te,'seconds'
n == 10000 

lxml
count: 3.0 
2.84083032899 seconds

re
count: 3 
0.141663256084 seconds