在python中使用lxml创建元素时出现“无效的标签名称”错误

 奥咨达医疗器_械丶服务集团 发布于 2023-02-13 17:19

我正在使用lxml制作xml文件,而我的示例程序是:

from lxml import etree
import datetime
dt=datetime.datetime(2013,11,30,4,5,6)
dt=dt.strftime('%Y-%m-%d')
page=etree.Element('html')
doc=etree.ElementTree(page)
dateElm=etree.SubElement(page,dt)
outfile=open('somefile.xml','w')
doc.write(outfile)

而且我得到以下错误输出:

dateElm=etree.SubElement(page,dt)
  File "lxml.etree.pyx", line 2899, in lxml.etree.SubElement (src/lxml/lxml.etree.c:62284)
  File "apihelpers.pxi", line 171, in lxml.etree._makeSubElement (src/lxml/lxml.etree.c:14296)
  File "apihelpers.pxi", line 1523, in lxml.etree._tagValidOrRaise (src/lxml/lxml.etree.c:26852)
ValueError: Invalid tag name u'2013-11-30'

I thought it of a Unicode Error, so tried changing encoding of 'dt' with codes like

    str(dt)

    unicode(dt).encode('unicode_escape')

    dt.encocde('ascii','ignore')

    dt.encode('ascii','decode')

and some others also, but none worked and same error msg generated.

1 个回答
  • You get the error because element names are not allowed to begin with a digit in XML. See http://www.w3.org/TR/xml/#sec-common-syn and http://www.w3.org/TR/xml/#sec-starttags. The first character of a name must be a NameStartChar, which disallows digits.

    诸如之类的元素<2013-11-30>...</2013-11-30>无效。

    诸如<D2013-11-30>...</D2013-11-30>OK之类的元素。

    如果将程序更改为使用ElementTree而不是lxml(from xml.etree import ElementTree as etree而不是from lxml import etree),则没有错误。但我会认为是一个错误。lxml做正确的事情,ElementTree做不正确。

    2023-02-13 17:21 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有