首页 > 解决方案 > 使用 xml 元素树解析 Errno22

问题描述

我正在尝试开发一种简单的网络抓取工具,并且一直遇到使用的 XML 文件的解析代码问题。

每当我运行它时,它都会给我 Errno22,即使路径是有效的。有人可以帮忙吗?

try:
    xmlTree = ET.parse('C:\TestWork\RWPlus\test.xml')
    root = xmlTree.getroot()

    returnValue = root[tariffPOS][childPOS].text
    return returnValue
except Exception as error:
    errorMessage = "A " + str(
        error) + " error occurred when trying to read the XML file."
    ErrorReport(errorMessage)

标签: pythonelementtree

解决方案


您应该在 Python 字符串中转义反斜杠

ET.parse('C:\\TestWork\\RWPlus\\test.xml')

或者您可以使用原始字符串(注意r

ET.parse(r'C:\TestWork\RWPlus\test.xml')

推荐阅读