Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/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
Python 3.x 如何从下面提到的xml文件中获取所需的值?_Python 3.x_Xml - Fatal编程技术网

Python 3.x 如何从下面提到的xml文件中获取所需的值?

Python 3.x 如何从下面提到的xml文件中获取所需的值?,python-3.x,xml,Python 3.x,Xml,1)我想阅读下面提到的XML文件并访问这些值,我已经尝试了许多方法,但无法访问,例如,我想要“NightRaidPerformanceCPUScore”值,这是从哪个passIndex获取的。 <?xml version='1.0' encoding='utf8'?> <benchmark> <results> <result> <name /> <descr

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
C:\Users\dgadhipx\Documents\3DMark\3DMark-autosave-20200401155825.3DMark-result
2066
1454
2233
8045dec5-e97c-452b-abeb-54af187fd50a
0
C:\Users\dgadhipx\Documents\3DMark\3DMark-autosave-20200401155825.3DMark-result
1454
2066
2233
9.57
12.18
395.2
8045dec5-e97c-452b-abeb-54af187fd50a

您可以使用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”标记的值。

您可以使用BeautifulSoup模块,然后通过标记find_all(和标记名)查找您想要的所有元素,该标记将返回您所有匹配的标记。
我已经尝试了许多方法,但无法访问。
您应该显示您所尝试的内容。否则,人们会认为你只是想要免费的代码。欢迎你,请你投票并将问题标记为已回答。祝你好运,朋友!