xml - python xml读取文件
问题描述
我希望有人能帮忙。我无法读取 XML 文件并尝试了许多建议的解决方案。首先是文件:
<Program>
<20180531 />
<20180601 />
<20180602 />
<20180603 />
<20180604 />
<20180605 />
<20180606 />
<20180607 />
<20180608 />
<20180609 />
<20180610 />
<20180611 />
<20180612 />
<20180613 />
<20180614 />
<20180615 />
<20180616 />
<20180617 />
<20180618 />
<20180619 />
<20180620 />
<20180621 />
<20180622 />
<20180623 />
<20180624 />
<20180625 />
<20180626 />
<20180627 />
<20180628 />
<20180629 />
<20180630 />
<20180701 />
<20180702 />
<20180703 />
<20180704 />
</Program>
真的只是一些约会。该文件是通过调用生成的
import xml.etree.ElementTree as etree
top = etree.Element('Program')
for day in alldays: # alldays is a list of dates using datetime
dan = etree.SubElement(top, day.strftime('%Y%m%d'))
tree = etree.ElementTree(top)
tree.write(directory + omnifile)
但我无法通过调用读取此文件:
tree = etree.parse(somefile)
我收到此错误:xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 10
我没有看到问题,也无法理解为什么 ElementTree 生成的文件无法被 ElemenTree 读取。我在 Fedora 中使用 Python 3.6.5。
解决方案
“XML”文件格式不正确。元素名称不允许以数字开头。请注意,数字不包括NameStartChar
在 XML 语法的生成中:https ://www.w3.org/TR/xml/#NT-NameStartChar 。
ElementTree 在创建 XML 输出时有点松懈。另一方面,lxml不会让你这样做。如果您尝试,将会出现“Invalid tag name”错误。
推荐阅读
- r - 如何将 UCL 和 LCL 线添加到 R 中的图表中
- rest - 如何将 PowerShell 调用 webrequest 与 Windows 身份验证和用户名\密码一起使用
- php - 使用 PHP 将 CSV 文件转换为 SQL
- javascript - 在返回 Cloud Function 之前等待 Algolia 响应
- python - 问题:关闭端口后,python 仍然读取来自 arduino 的串行数据。串口无法关闭
- wordpress - 如何在woocommerce中自动发送带有立即付款链接的发票?
- ruby-on-rails - money-rails, Money#== 仅支持零数字
- python - Python:搜索和替换但忽略注释行
- r - mgcv:如何指定平滑和因子之间的交互?第二部分
- angular - Angular 5 - 在浏览器刷新时重定向到应用程序的根目录