python - 使用 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)
解决方案
您应该在 Python 字符串中转义反斜杠
ET.parse('C:\\TestWork\\RWPlus\\test.xml')
或者您可以使用原始字符串(注意r
)
ET.parse(r'C:\TestWork\RWPlus\test.xml')
推荐阅读
- jsf - 无法从同一 p:tab 中的另一个表单更新组件
- sql - 如何执行递归 SQL where 语句?
- oracle - 如何在 Gitlab-ci 中执行 sql 文件
- javascript - 刷新地图时防止从图层中删除标记
- javascript - 停止 Chrome 自动完成下拉菜单关注
- c# - 将 JIRA API cURL 转换为 c# http 请求
- angular - Tab 控件中的组件
- audiokit - 有没有办法对单独使用 AudioKit 的代码进行单元测试?
- rest - 如何在 Asterisk ARI 中引导发起 SIP?
- c++ - 在末尾移动一个容器的项目(双端队列)