Как получить необходимые значения из файла xml, указанного ниже? - PullRequest
0 голосов
/ 14 апреля 2020

1) Я хочу прочитать нижеупомянутый файл XML и получить доступ к значениям, я уже пытался многими способами, но не смог получить к нему доступ, например, я хочу значение 'NightRaidPerformanceCPUScore' и то, из которого passIndex.

<?xml version='1.0' encoding='utf8'?>
<benchmark>
    <results>
        <result>
            <name />
            <description />
            <passIndex>-1</passIndex>
            <sourceId>C:\Users\dgadhipx\Documents\3DMark\3dmark-autosave-20200401155825.3dmark-result</sourceId>
            <NightRaidPerformance3DMarkScore>2066</NightRaidPerformance3DMarkScore>
            <NightRaidPerformanceCPUScore>1454</NightRaidPerformanceCPUScore>
            <NightRaidPerformanceGraphicsScore>2233</NightRaidPerformanceGraphicsScore>
            <benchmarkRunId>8045dec5-e97c-452b-abeb-54af187fd50a</benchmarkRunId>
        </result>
        <result>
            <name />
            <description />
            <passIndex>0</passIndex>
            <sourceId>C:\Users\dgadhipx\Documents\3DMark\3dmark-autosave-20200401155825.3dmark-result</sourceId>
            <NightRaidPerformanceCPUScoreForPass>1454</NightRaidPerformanceCPUScoreForPass>
            <NightRaidPerformance3DMarkScoreForPass>2066</NightRaidPerformance3DMarkScoreForPass>
            <NightRaidPerformanceGraphicsScoreForPass>2233</NightRaidPerformanceGraphicsScoreForPass>
            <NightRaidPerformanceGraphicsTest1>9.57</NightRaidPerformanceGraphicsTest1>
            <NightRaidPerformanceGraphicsTest2>12.18</NightRaidPerformanceGraphicsTest2>
            <NightRaidCpuP>395.2</NightRaidCpuP>
            <benchmarkRunId>8045dec5-e97c-452b-abeb-54af187fd50a</benchmarkRunId>
        </result>
    </results>
</benchmark>

1 Ответ

0 голосов
/ 14 апреля 2020

Вы можете использовать BeautifulSoup как собеседника:

with open(file_path, "r") as f:
     content = f.read()
xml = BeautifulSoup(content, 'xml')
elements = xml.find_all("NightRaidPerformanceCPUScore")
for i in elements:
    print(i.text)

Это напечатает вам значения всех тегов "NightRaidPerformanceCPUScore".

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...