0
我可以返回一個字符串列表,如果我映射到元素屬性每個元素的字符串:返回使用子串後
In [15]: root
Out[15]: <Element ADI at 0x7f4b5e6ad188>
In [16]: root = et.fromstring('<a><c><b value="the.never.ending.tale.99999.gz"/><b value="the.never.ending.tale.999745745799.gz"/><b value="the.never.ending.tale.99951351399.gz"/></c></a>')
In [23]: root.xpath('./c/b/@value')
Out[23]:
['the.never.ending.tale.99999.gz',
'the.never.ending.tale.999745745799.gz',
'the.never.ending.tale.99951351399.gz']
但是我可以用不返回字符串的列表:
In [24]: root.xpath('./c/b/substring-after(@value,"never")')
---------------------------------------------------------------------------
XPathEvalError Traceback (most recent call last)
<ipython-input-24-16664f97a745> in <module>()
----> 1 root.xpath('./c/b/substring-after(@value,"never")')
src/lxml/lxml.etree.pyx in lxml.etree._Element.xpath (src/lxml/lxml.etree.c:57924)()
src/lxml/xpath.pxi in lxml.etree.XPathElementEvaluator.__call__ (src/lxml/lxml.etree.c:167085)()
src/lxml/xpath.pxi in lxml.etree._XPathEvaluatorBase._handle_result (src/lxml/lxml.etree.c:166044)()
XPathEvalError: Invalid expression
我在這裏犯了什麼錯誤?
你要不能XPath 1.0中做些什麼;我只需要列表理解就能得到你想要的子字符串。 – stranac