我正在使用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)
Run Code Online (Sandbox Code Playgroud)
而且我得到以下错误输出:
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'
Run Code Online (Sandbox Code Playgroud)
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.