xmlファイルの最後のエントリを読み込み、その値を取得します。ここで私は、ファイルの最後のタグで<total>
、<totalpass>
、<totalfail>
と<totalerror>
値を取得したいPythonでファイルから最後のエントリを読み取るXML
<TestSuite>
<TestCase>
<name>tcname1</name>
<total>1</total>
<totalpass>0</totalpass>
<totalfail>0</totalfail>
<totalerror>1</totalerror>
</TestCase>
<TestCase>
<name>tcname2</name>
<total>1</total>
<totalpass>0</totalpass>
<totalfail>0</totalfail>
<totalerror>1</totalerror>
</TestCase>
</TestSuite>
私のxmlファイルです。私はこのコードを試してみました。
import xmltodict
with open(filename) as fd:
doc = xmltodict.parse(fd.read())
length=len(doc['TestSuite']['TestCase'])
tp=doc['TestSuite']['TestCase'][length-1]['totalpass']
tf=doc['TestSuite']['TestCase'][length-1]['totalfail']
te=doc['TestSuite']['TestCase'][length-1]['totalerror']
total=doc['TestSuite']['TestCase'][length-1]['total']
これは、XMLファイル内の2個の以上のテストケースタグとXMLのために働く、しかし、唯一のテストケースタグを持つファイルに対して、このエラーで失敗します。
Traceback (most recent call last):
File "HTMLReportGenerationFromXML.py", line 52, in <module>
tp=doc['TestSuite']['TestCase'][length-1]['totalpass']
KeyError: 4 .
カウントではなく、サブタグ(長さなどの値)を取っているためです。この問題を解決するのを手伝ってください。
これはうまく動作します。ありがとうございます。 –
ようこそ。あなたが最も役立つと思われる答えをupvoteおよび/または受け入れてください。 –