首页 > 解决方案 > 无法使用 python 从路径解析 XML 文件

问题描述

当我尝试解析与脚本不在同一目录中的 xml 文件时,出现此错误:

File "C:\Users\Yac\AppData\Local\Programs\Python\Python37\lib\xml\etree\ElementTree.py", line 1197, in parse
        tree.parse(source, parser)
      File "C:\Users\Yac\AppData\Local\Programs\Python\Python37\lib\xml\etree\ElementTree.py", line 598, in parse
        self._root = parser._parse_whole(source)
    xml.etree.ElementTree.ParseError: syntax error: line 1, column 0

这是我的代码:

import xml.etree.ElementTree as ET
tree = ET.parse(r'C:\Users\Yac\Desktop\AAAAA\ABD\DATA.xml')

标签: python-3.xxml

解决方案


尝试使用tree = ET.parse(“path/to/file.xml”)ET.fromstring(open(‘path/to/file.xml’).read())


推荐阅读