print(type(players[1]))
for player in players:
player = str(player)
print(type(players[1]))
输出是:
<class 'lxml.etree._ElementUnicodeResult'>
<class 'lxml.etree._ElementUnicodeResult'>
我需要将这个ElementUnicode对象转换为字符串,以便我可以在其上调用substring,但是这个for循环不会这样做。当我将它转换为字符串并同时调用substring时,控制台会读取:str对象没有对象子字符串,这没有任何意义。
str(players[i]).substring()
有关为什么会出现这种情况以及如何绕过此对象调用子字符串的任何想法?
答案 0 :(得分:1)
将_ElementUnicodeResult
投射到str
没问题。完整的演示:
from lxml import etree
doc = """
<root>
<player>ABC</player>
<player>DEF</player>
</root>"""
root = etree.fromstring(doc)
players = root.xpath("player/text()")
print(type(players[1]))
# Cast each player to a plain string
players = [str(p) for p in players]
print(type(players[1]))
输出:
<class 'lxml.etree._ElementUnicodeResult'>
<class 'str'>
在问题中,您具有以下代码:
for player in players:
player = str(player)
print(type(players[1]))
这不会更改for
循环之外的任何内容。 type(players[1])
与以前相同。
此外,确实str
没有substring
属性。 XPath具有substring()
函数。也许这就是您的想法?
答案 1 :(得分:0)
如果您正在运行python2,请尝试:unicode(players[i])