使用Python从XML文件中名称为“:”的标记获取值

2024-04-26 07:45:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要从xml文件中提取值,这与“get”函数并不复杂。但是,标记名为“ext:numericValue”,而“:”似乎是个问题。我不知道怎么过马路

我对XML文件感兴趣的一行如下

<dataIndicator ext:numericValue="1.502144031" name="First index">

我尝试提取数值1.502144031(或字符串值,这无关紧要) 使用此代码:

index = criteria.get("name")
if index == "First index":
    first_index = criteria.get("ext:numericValue")
print(first_index)

它无法识别标记“ext:numericValue”,并导致 使用get函数调用时为“None”

顺便说一句,它与索引一起工作

print(index)
>>"First index"

我不知道如何跨越这个问题


Tags: 文件函数name标记getindexxmlext