![](/img/trans.png)
[英]Remove common member from two list in python3 with types_linq module
[英]Count specific tag in XML file in python3 with Enumerable class in types_linq module
我有一個 XML 文件,它是 Valgrind 輸出。 我想計算 XML 文件中的特定標簽。 我寫了下面的代碼來計算 XML 文件中的錯誤標簽。例外是TypeError: 'xml.etree.ElementTree.Element' object is not callable
。
代碼是:
xml_file='maria_valgrind.xml'
tree = Et.parse(xml_file)
root = tree.getroot()
all_child = Enumerable(root)
count_total_errors = all_child.where(lambda m: m.tag == 'error').count()
例如 maria_valgrind.xml 文件是:
<?xml version="1.0"?>
<valgrindoutput>
<protocolversion>4</protocolversion>
<protocoltool>memcheck</protocoltool>
<pid>10219</pid>
<ppid>10200</ppid>
<tool>memcheck</tool>
<status>
<state>RUNNING</state>
<time>00:00:00:00.052 </time>
</status>
<error>
<unique>0x4</unique>
<tid>1</tid>
<kind>Leak_StillReachable</kind>
<xwhat>
<text>384 bytes in 1 blocks are still reachable in loss record 5 of 6</text>
<leakedbytes>384</leakedbytes>
<leakedblocks>1</leakedblocks>
</xwhat>
<stack>
</stack>
</error>
</valgrindoutput>
我有一個 XML 文件,它是 Valgrind 輸出。 我想計算 XML 文件中的特定標簽。
假設您想在文檔的任何深度計算錯誤標簽,下面的代碼就可以做到。
import xml.etree.ElementTree as ET
xml = '''<?xml version="1.0"?>
<valgrindoutput>
<protocolversion>4</protocolversion>
<protocoltool>memcheck</protocoltool>
<pid>10219</pid>
<ppid>10200</ppid>
<tool>memcheck</tool>
<status>
<state>RUNNING</state>
<time>00:00:00:00.052 </time>
</status>
<error>
<unique>0x4</unique>
<tid>1</tid>
<kind>Leak_StillReachable</kind>
<xwhat>
<text>384 bytes in 1 blocks are still reachable in loss record 5 of 6</text>
<leakedbytes>384</leakedbytes>
<leakedblocks>1</leakedblocks>
</xwhat>
<stack>
</stack>
</error>
</valgrindoutput>'''
root = ET.fromstring(xml)
num_of_err_tags = len(root.findall('.//error'))
print(f'Number of error tags is: {num_of_err_tags}')
輸出
Number of error tags is: 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.