擅长:python、mysql、java
<p>将<code>_ElementUnicodeResult</code>转换为<code>str</code>不是问题。完整演示:</p>
<pre><code>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]))
</code></pre>
<p>输出:</p>
^{pr2}$
<hr/>
<p>在问题中,您有以下代码:</p>
<pre><code>for player in players:
player = str(player)
print(type(players[1]))
</code></pre>
<p>这不会更改<code>for</code>循环之外的任何内容。<code>type(players[1])</code>将与之前相同。在</p>
<p>另外,<code>str</code>没有<code>substring</code>属性。XPath有一个<code>substring()</code>函数。也许你就是这么想的?在</p>