Python3使用ElementTree解析xml文件
帮助,我有下面的XML文件,我正试图从中读取和提取数据,下面是XML文件的摘录Python3使用ElementTree解析xml文件,python,python-3.x,xml-parsing,elementtree,xml.etree,Python,Python 3.x,Xml Parsing,Elementtree,Xml.etree,帮助,我有下面的XML文件,我正试图从中读取和提取数据,下面是XML文件的摘录 17.154、17.154、17.154、17.154、17.154、17.154、17.154、17.154、17.154、17.154、17.154、17.154、17.154、17.154250FalseTrueTrue 我试图提取以下内容,17.154,17.154,17.154,17.154,17.154,17.154,17.154,17.154,17.154,17.154,17.154,17.154,17
17.154、17.154、17.154、17.154、17.154、17.154、17.154、17.154、17.154、17.154、17.154、17.154、17.154、17.154250FalseTrueTrue
我试图提取以下内容,17.154,17.154,17.154,17.154,17.154,17.154,17.154,17.154,17.154,17.154,17.154,17.154,17.154,17.154
我已经完成了这里的示例,我可以让示例工作,但是当我修改上面xml的代码时,代码不会返回任何结果
这是我的密码
import xml.etree.ElementTree as ET
work_dir = r"C:\Temp\APROCONE\Python"
with open(model.xml, 'rt') as f:
tree = ET.parse(f)
root = tree.getroot()
for Variable in root.findall('Variable'):
type = Variable.find('type').text
name = Variable.get('name')
print(name, type)
有什么想法吗?提前感谢您的帮助
编辑:
感谢所有发表评论的人。根据你的建议,我进行了一次游戏和搜索,得到了以下代码
with open(os.path.join(work_dir, "output.txt"), "w") as f:
for child1Tag in root.getchildren():
for child2Tag in child1Tag.getchildren():
for child3Tag in child2Tag.getchildren():
for child4Tag in child3Tag.getchildren():
for child5Tag in child4Tag.getchildren():
name = child5Tag.get('name')
if name == "Inboard_ED_mm":
print(child5Tag.attrib, file=f)
print(name, file=f)
print(child5Tag.text, file=f)
要返回以下内容
{'name': 'Inboard_ED_mm', 'state': 'Output', 'type': 'double[]'}
Inboard_ED_mm
17.154, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154, 17.154
我知道,这不是世界上最好的代码,任何关于如何简化代码的想法都是非常受欢迎的。您的根节点已经是
变量
标记,因此您将找不到任何带有变量
标记和findall
的内容,它只能搜索子节点。您只需输出根节点的text
属性即可:
print(root.text)
您的根节点已经是
Variable
标记,因此您将找不到带有Variable
标记和findall
的任何内容,该标记只能搜索子节点。您只需输出根节点的text
属性即可:
print(root.text)
您说上面是XML文件的“摘录”。XML的结构很重要。上述内容是否仅位于根节点内部
for Variable in root.findall('Variable'):
print(Variable.get('name'), Variable.text)
或者它是否存在于XML树结构的某个更深层的已知级别
for Variable in root.findall('Path/To/Variable'):
print(Variable.get('name'), Variable.text)
或者它是否存在于XML树结构中某个未指定的更深层次
for Variable in root.findall('.//Variable'):
print(Variable.get('name'), Variable.text)
演示最后两个:
>>> import xml.etree.ElementTree as ET
>>> src = """
<root>
<SubNode>
<Variable name='x'>17.154, ..., 17.154<Properties>...</Properties></Variable>
<Variable name='y'>14.174, ..., 15.471<Properties>...</Properties></Variable>
</SubNode>
</root>"""
>>> root = ET.fromstring(src)
>>> for Variable in root.findall('SubNode/Variable'):
print(Variable.get('name'), Variable.text)
x 17.154, ..., 17.154
y 14.174, ..., 15.471
>>>
>>> for Variable in root.findall('.//Variable'):
print(Variable.get('name'), Variable.text)
x 17.154, ..., 17.154
y 14.174, ..., 15.471
或
使用标记1到4的精确标记名,我们可以为您提供更精确的XPath,而不是依赖*/*/*/*/
您说的上述内容是XML文件的“摘录”。XML的结构很重要。上述内容是否仅位于根节点内部
for Variable in root.findall('Variable'):
print(Variable.get('name'), Variable.text)
或者它是否存在于XML树结构的某个更深层的已知级别
for Variable in root.findall('Path/To/Variable'):
print(Variable.get('name'), Variable.text)
或者它是否存在于XML树结构中某个未指定的更深层次
for Variable in root.findall('.//Variable'):
print(Variable.get('name'), Variable.text)
演示最后两个:
>>> import xml.etree.ElementTree as ET
>>> src = """
<root>
<SubNode>
<Variable name='x'>17.154, ..., 17.154<Properties>...</Properties></Variable>
<Variable name='y'>14.174, ..., 15.471<Properties>...</Properties></Variable>
</SubNode>
</root>"""
>>> root = ET.fromstring(src)
>>> for Variable in root.findall('SubNode/Variable'):
print(Variable.get('name'), Variable.text)
x 17.154, ..., 17.154
y 14.174, ..., 15.471
>>>
>>> for Variable in root.findall('.//Variable'):
print(Variable.get('name'), Variable.text)
x 17.154, ..., 17.154
y 14.174, ..., 15.471
或
使用标记1到4的精确标记名,我们可以为您提供更精确的XPath,而不是依赖
*/*/*/*/
“下面是xml文件的摘录”-问题可能是变量
位于默认命名空间中。您是否在XML中有一个未显示的xmlns=“?”
位置?@Daniel Haley,感谢您的回复,抱歉,我在文件中找不到“xmlns”。“下面是XML文件的摘录”-问题可能是变量
位于默认命名空间中。您在XML中是否有未显示的xmlns=“?”
任何地方?@Daniel Haley,感谢您的回复,抱歉,我在文件中找不到“xmlns”。感谢您的回复,您的回复帮助我找到了解决方案。感谢您的回复,您的回复帮助我找到了解决方案。感谢您的回复,经过一段时间的播放和搜索,它位于根节点的深处!我已经在原始问题中添加了代码。啊-您正在查找具有确切名称
属性的变量
标记。这有一个XPath。请参阅更新。感谢您的回复,经过一段时间的播放和搜索,它位于根节点的深处!我已经在原始问题中添加了代码。啊-您正在查找具有确切名称
属性的变量
标记。这有一个XPath。请参阅更新。